Category Archives: IOS

ios入門–ストレージの容量を取得する

サンプルコード:

+ (CGFloat)diskOfAllSizeMBytes{
CGFloat size = 0.0;
NSError *error;
NSDictionary *dic = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
if (error) {
#ifdef DEBUG
NSLog(@”error: %@”, error.localizedDescription);
#endif
}else{
NSNumber *number = [dic objectForKey:NSFileSystemSize];
size = [number floatValue]/1024/1024;
}
return size;
}

ios入門–配列オブジェクトからjson文字列に変換する

サンプルコード
+ (NSString *)jsonPrettyStringEncoded:(NSArray *)array{
if ([NSJSONSerialization isValidJSONObject:array]) {
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error];
if (!error) {
NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
return json;
}
}
return nil;
}

ios開発–NSDateとNSCalendarの使い方

1.NSDateの利用方法
NSDate *now = [NSDate date];
NSLog(@”%@”,now);

//now+20秒
NSDate *now1 = [now dateByAddingTimeInterval:20];
NSLog(@”%@”,now1);

//システムのタイムゾーンを取得
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
NSLog(@”timeZone = %@”,timeZone);

//現在のゾーンと指定ゾーンの時刻差
NSInteger seconds =[timeZone secondsFromGMTForDate:now];
NSLog(@”seconds = %ld”,seconds);

//時間フォーマット
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @”yyyy-MM-dd hh-mm-ss Z”;

//String -> date
NSString *now1 = @”2016-05-14 03:16:28 +0011″;
NSLog(@”%@”,[formatter dateFromString:now1]);
NSDate *now = [NSDate date];

2. NSCalendarの利用方法
NSCalendar *calender = [NSCalendar currentCalendar];
NSCalendarUnit type = NSCalendarUnitYear |
NSCalendarUnitMonth |
NSCalendarUnitDay |
NSCalendarUnitHour |
NSCalendarUnitMinute |
NSCalendarUnitSecond;
NSDateComponents *cmps = [calender components:type fromDate:now];
NSLog(@”year = %ld”, cmps.year);
NSLog(@”month = %ld”, cmps.month);
NSLog(@”day = %ld”, cmps.day);
NSLog(@”hour = %ld”, cmps.hour);
NSLog(@”minute = %ld”, cmps.minute);
NSLog(@”second = %ld”, cmps.second);

//時間比較
NSString *str = @”2015-05-21 07:05:26 +0000″;
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = @”yyyy-MM-dd HH:mm:ss Z”;
NSDate *date = [formatter dateFromString:str];
//現在の時間を取得
NSDate *now1 = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *component1 = [calendar components:type fromDate:date toDate:now1 options:0];
NSLog(@”%ld年%ld月%ld日%ld時%ld分%ld秒”, component1.year, component1.month, component1.day, component1.hour, component1.minute, component1.second);

「Objective-C」iosデバイスのIPアドレスを取得する方法

参考コード:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/sockio.h>
#include <net/if.h>
#include <errno.h>
#include <net/if_dl.h>
//#include “GetAddresses.h”
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))
#define BUFFERSIZE 4000
char *if_names[MAXADDRS];
char *ip_names[MAXADDRS];
char *hw_addrs[MAXADDRS];
unsigned long ip_addrs[MAXADDRS];
static int nextAddr = 0;
void InitAddresses()
{
int i;
for (i=0; i<MAXADDRS; ++i)
{
if_names[i] = ip_names[i] = hw_addrs[i] = NULL;
ip_addrs[i] = 0;
}
}
void FreeAddresses()
{
int i;
for (i=0; i<MAXADDRS; ++i)
{
if (if_names[i] != 0) free(if_names[i]);
if (ip_names[i] != 0) free(ip_names[i]);
if (hw_addrs[i] != 0) free(hw_addrs[i]);
ip_addrs[i] = 0;
}
InitAddresses();
}
void GetIPAddresses()
{
int i, len, flags;
char buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;
struct ifconf ifc;
struct ifreq *ifr, ifrcopy;
struct sockaddr_in *sin;
char temp[80];
int sockfd;
for (i=0; i<MAXADDRS; ++i)
{
if_names[i] = ip_names[i] = NULL;
ip_addrs[i] = 0;
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
{
perror(“socket failed”);
return;
}
ifc.ifc_len = BUFFERSIZE;
ifc.ifc_buf = buffer;
if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
{
perror(“ioctl error”);
return;
}
lastname[0] = 0;
for (ptr = buffer; ptr < buffer + ifc.ifc_len; )
{
ifr = (struct ifreq *)ptr;
len = max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len);
ptr += sizeof(ifr->ifr_name) + len; // 次のバッファ
if (ifr->ifr_addr.sa_family != AF_INET)
{
continue; // ignore if not desired address family
}
if ((cptr = (char *)strchr(ifr->ifr_name, ‘:’)) != NULL)
{
*cptr = 0; // コロンを交換してnull結果
}
if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0)
{
continue; /* インタフェースを処理済み */
}
memcpy(lastname, ifr->ifr_name, IFNAMSIZ);
ifrcopy = *ifr;
ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);
flags = ifrcopy.ifr_flags;
if ((flags & IFF_UP) == 0)
{
continue; //インターフェースを無視
}
if_names[nextAddr] = (char *)malloc(strlen(ifr->ifr_name)+1);
if (if_names[nextAddr] == NULL)
{
return;
}
strcpy(if_names[nextAddr], ifr->ifr_name);
sin = (struct sockaddr_in *)&ifr->ifr_addr;
strcpy(temp, inet_ntoa(sin->sin_addr));
ip_names[nextAddr] = (char *)malloc(strlen(temp)+1);
if (ip_names[nextAddr] == NULL)
{
return;
}
strcpy(ip_names[nextAddr], temp);
ip_addrs[nextAddr] = sin->sin_addr.s_addr;
++nextAddr;
}
close(sockfd);
}
void GetHWAddresses()
{
struct ifconf ifc;
struct ifreq *ifr;
int i, sockfd;
char buffer[BUFFERSIZE], *cp, *cplim;
char temp[80];
for (i=0; i<MAXADDRS; ++i)
{
hw_addrs[i] = NULL;
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
{
perror(“socket failed”);
return;
}
ifc.ifc_len = BUFFERSIZE;
ifc.ifc_buf = buffer;
if (ioctl(sockfd, SIOCGIFCONF, (char *)&ifc) < 0)
{
perror(“ioctlエラー”);
close(sockfd);
return;
}
ifr = ifc.ifc_req;
cplim = buffer + ifc.ifc_len;
for (cp=buffer; cp < cplim; )
{
ifr = (struct ifreq *)cp;
if (ifr->ifr_addr.sa_family == AF_LINK)
{
struct sockaddr_dl *sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
int a,b,c,d,e,f;
int i;
strcpy(temp, (char *)ether_ntoa(LLADDR(sdl)));
sscanf(temp, “%x:%x:%x:%x:%x:%x”, &a, &b, &c, &d, &e, &f);
sprintf(temp, “%02X:%02X:%02X:%02X:%02X:%02X”,a,b,c,d,e,f);
for (i=0; i<MAXADDRS; ++i)
{
if ((if_names[i] != NULL) && (strcmp(ifr->ifr_name, if_names[i]) == 0))
{
if (hw_addrs[i] == NULL)
{
hw_addrs[i] = (char *)malloc(strlen(temp)+1);
strcpy(hw_addrs[i], temp);
break;
}
}
}
}
cp += sizeof(ifr->ifr_name) + max(sizeof(ifr->ifr_addr), ifr->ifr_addr.sa_len);
}
close(sockfd);
}
– (NSString *)deviceIPAdress {
InitAddresses();
GetIPAddresses();
GetHWAddresses();
return [NSString stringWithFormat:@”%s”, ip_names[1]];
}

ios開発でemojiのサポート判定、有効無効の操作方法

1.絵文字をサポートするかどうかを判断
– (BOOL)supportEmoji
{
BOOL hasEmoji = NO;
#define kPreferencesPlistPath @”/private/var/mobile/Library/Preferences/com.apple.Preferences.plist”
NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:kPreferencesPlistPath];
NSNumber *emojiValue = [plistDict objectForKey:@”KeyboardEmojiEverywhere”]; if (emojiValue)
hasEmoji = YES;
else
hasEmoji = NO;
[plistDict release];

return hasEmoji;
}

2.有効または無効
– (void)valueControl:(BOOL)open
{
#define kPreferencesPlistPath @”/private/var/mobile/Library/Preferences/com.apple.Preferences.plist”
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:kPreferencesPlistPath];
[plistDict setValue:[NSNumber numberWithBool:open] forKey:@”KeyboardEmojiEverywhere”];
[plistDict writeToFile:kPreferencesPlistPath atomically:NO];
[plistDict release];
}

ios開発でタッチスクリーン操作の画面で上下または左右にスライドさせるサンプルコード

サンプルコード
infoView=[[UIView alloc] initWithFrame:CGRectMake(25, 110,210, 270)];
infoView.backgroundColor=[UIColor blueColor];
infoView.alpha=0.6;
[self.view addSubview:infoView];
/******************ジェスチャー監視*****************/
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[infoView addGestureRecognizer:recognizer];
// recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
// [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
// [self.view addGestureRecognizer:recognizer];
// recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
// [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
// [self.view addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[infoView addGestureRecognizer:recognizer];
//タッチイベント機能の実現
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft) {
NSLog(@”****************左スライド****************”);
}
if(recognizer.direction==UISwipeGestureRecognizerDirectionRight) {
NSLog(@”****************右スライド****************”);
}
if (recognizer.direction==UISwipeGestureRecognizerDirectionDown){
NSLog(@”****************下スライド****************”);
}
if (recognizer.direction==UISwipeGestureRecognizerDirectionUp){
NSLog(@”****************上スライド****************”);
}
}

iosで2つのファイルは同じかどうか判断方法

サンプルコード
if ([fileManager contentsEqualAtPath:@”FilePath_startnews24aa” andPath:@” FilePath_startnews24bb”]) {
NSLog(@”2つのファイルの差分がございません。”);
}

ios開発にキーボードを好きな色に変更するサンプルコード

サンプルコード:
– (void)textFieldDidBeginEditing:(UITextField *)textField{
NSArray *ws = [[UIApplication sharedApplication] windows];
for(UIView *w in ws){
NSArray *vs = [w subviews];
for(UIView *v in vs){
if([[NSString stringWithUTF8String:object_getClassName(v)] isEqualToString:@”UIPeripheralHostView”]){
v.backgroundColor = [UIColor redColor];
}
}
}
}

ios開発にプッシュ通知を実装する方法

1.Push通知
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:15]; if (notification != nil) {
// push time
notification.fireDate = pushDate;
// set zone
notification.timeZone = [NSTimeZone defaultTimeZone];
// 時間間隔
notification.repeatInterval = kCFCalendarUnitDay;
// push sound
notification.soundName = UILocalNotificationDefaultSoundName;
// push content
notification.alertBody = @”arkgame.com provide it”; notification.applicationIconBadgeNumber = 1;
NSDictionary *info = [NSDictionary dictionaryWithObject:@”name”forKey:@”key”];
notification.userInfo = info;
//push add UIApplication
UIApplication *app = [UIApplication sharedApplication]; [app scheduleLocalNotification:notification];
}

2.プッシュ通知の取得
– (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”iWeibo” message:notification.alertBody delegate:nil cancelButtonTitle:@”ok” otherButtonTitles:nil];
[alert show];
application.applicationIconBadgeNumber -= 1;
}

3.プッシュ通知の解除
// UIApplicationの取得
UIApplication *app = [UIApplication sharedApplication];
//プッシュの配列を取得
NSArray *localArray = [app scheduledLocalNotifications];
//通知オブジェクトの宣言
UILocalNotification *localNotification;
if (localArray) {
for (UILocalNotification *noti in localArray) {
NSDictionary *dict = noti.userInfo;
if (dict) {
NSString *inKey = [dict objectForKey:@”key”];
if ([inKey isEqualToString:@”key value”]) {
if (localNotification){
[localNotification release];
localNotification = nil;
}
localNotification = [noti retain];
break;
}
}
}
//同じキーが既に存在するかどうか
if (!localNotification) {
localNotification = [[UILocalNotification alloc] init];
}
if (localNotification) {
//プッシュの解除
[app cancelLocalNotification:localNotification];
[localNotification release];
return;
}
}

「ios開発」ファイルを削除するサンプルコード

サンプルコード
NSFileManager *defaultManager;
defaultManager = [NSFileManager defaultManager];
[defaultManager removeFileAtPath: tildeFilenamehandler: nil]

3 / 2212345...1020...最後 »