推荐几个IOS常用宏定义

一、判断设备
01 //设备名称
02 return [UIDevice currentDevice].name;
03  
04 //设备型号,只可得到是何设备,无法得到是第几代设备
05 return [UIDevice currentDevice].model;
06  
07 //系统版本型号,如iPhone OS
08 return [UIDevice currentDevice].systemVersion;
09  
10 //系统版本名称,如6.1.3
11 return [UIDevice currentDevice].systemName;
01 //判断是否为iPhone
02 #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
03  
04 //判断是否为iPad
05 #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
06  
07 //判断是否为ipod
08 #define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])
09  
10 //判断是否为iPhone5
11 #define IS_IPHONE_5_SCREEN [[UIScreen mainScreen] bounds].size.height >= 568.0f && [[UIScreen mainScreen] bounds].size.height < 1024.0f
二、判断网络连接状态

1、导入SystemConfiguration.framework,并#import<SystemConfiguration/SCNetworkReachability.h>

2、判断设备是否联网

01 + (BOOL)connectedToNetwork{
02      
03     //创建零地址,0.0.0.0的地址表示查询本机的网络连接状态
04      
05     struct sockaddr_storage zeroAddress;
06      
07     bzero(&zeroAddress, sizeof(zeroAddress));
08     zeroAddress.ss_len = sizeof(zeroAddress);
09     zeroAddress.ss_family = AF_INET;
10      
11     // Recover reachability flags
12     SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
13     SCNetworkReachabilityFlags flags;
14      
15     //获得连接的标志
16     BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
17     CFRelease(defaultRouteReachability);
18      
19     //如果不能获取连接标志,则不能连接网络,直接返回
20     if (!didRetrieveFlags)
21     {
22         return NO;
23     }
24     //根据获得的连接标志进行判断
25  
26     BOOL isReachable = flags & kSCNetworkFlagsReachable;
27     BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
28     return (isReachable&&!needsConnection) ? YES : NO;
29 }
三、设备不显示的解决办法

PS:如果设备已经插上,但是XCode不显示当前设备,仍为iOS Device的状态,如下图所示

解决步骤

1.点击XCode右上角的Organizer->Devices,看自己的设备是否为绿灯状态,如果为黄灯或是没有灯,说明设备未连接好,请重新再插一次

2.如果设备已经连接好,但是还是不显示,则说明是系统版本不匹配,如果设备是ios5的系统,则XCode的iOS Deployment Target 必须是5.0或以上,XCode才可分辨此设备

选中TARGETS->Summary-Deployment Target 修改iOS版本号(假设设为6.1)

如果还是不识别,在PROJECT->Info->iOS Deployment Target下将版本号也修改为6.1



https://github.com/zhangxigithub/ZXMacro/blob/master/ZXMacro.h

/*

 

 ZXMacro.h

 

 使用方法:

 *需要时import

 *添加到-Prefix.pch文件中,所有类中都可以使用

 

 http://zhangxi.me

 https://github.com/zhangxigithub/ZXMicro

 

 2013.3.26

 */



//------------------------------------Debug/Release

#ifdef DEBUG

//Debug模式

//...



#else

//发布模式

//...


//屏蔽NSLog

#define NSLog(...) {};


#endif



//------------------------------------Simulator/Device

//区分模拟器和真机

#if TARGET_OS_IPHONE

//iPhone Device

#endif


#if TARGET_IPHONE_SIMULATOR

//iPhone Simulator

#endif


//------------------------------------ARC/no RAC

//ARC

#if __has_feature(objc_arc)

//compiling with ARC

#else

// compiling without ARC

#endif


//Block

typedef void(^VoidBlock)();

typedef BOOL(^BoolBlock)();

typedef int (^IntBlock) ();

typedef id  (^IDBlock)  ();


typedef void(^VoidBlock_int)(int);

typedef BOOL(^BoolBlock_int)(int);

typedef int (^IntBlock_int) (int);

typedef id  (^IDBlock_int)  (int);


typedef void(^VoidBlock_string)(NSString*);

typedef BOOL(^BoolBlock_string)(NSString*);

typedef int (^IntBlock_string) (NSString*);

typedef id  (^IDBlock_string)  (NSString*);


typedef void(^VoidBlock_id)(id);

typedef BOOL(^BoolBlock_id)(id);

typedef int (^IntBlock_id) (id);

typedef id  (^IDBlock_id)  (id);



//System

#define PasteString(string)   [[UIPasteboard generalPasteboard] setString:string];

#define PasteImage(image)     [[UIPasteboard generalPasteboard] setImage:image];



//Image

//可拉伸的图片


#define ResizableImage(name,top,left,bottom,right) [[UIImage imageNamed:name] resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right)]

#define ResizableImageWithMode(name,top,left,bottom,right,mode) [[UIImage imageNamed:name] resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right) resizingMode:mode]


//file

//读取文件的文本内容,默认编码为UTF-8

#define FileString(name,ext)            [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)] encoding:NSUTF8StringEncoding error:nil]

#define FileDictionary(name,ext)        [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)]]

#define FileArray(name,ext)             [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(name) ofType:(ext)]]


//数学

#define PI 3.14159


//输出frame(frame是结构体,没法%@)

#define LOGFRAME(f) NSLog(@"\nx:%f\ny:%f\nwidth:%f\nheight:%f\n",f.origin.x,f.origin.y,f.size.width,f.size.height)

#define LOGBOOL(b)  NSLog(@"%@",b?@"YES":@"NO");

//弹出信息

#define ALERT(msg) [[[UIAlertView alloc] initWithTitle:nil message:msg delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil] show]



//App

#define kApp ((AppDelegate *)[UIApplication sharedApplication].delegate)

#define kNav ((AppDelegate *)[UIApplication sharedApplication].delegate.navigationController)



//color

#define RGB(r, g, b)             [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:1.0]

#define RGBAlpha(r, g, b, a)     [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:(a)]


#define HexRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

#define HexRGBAlpha(rgbValue,a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:(a)]


//转换

#define I2S(number) [NSString stringWithFormat:@"%d",number]

#define F2S(number) [NSString stringWithFormat:@"%f",number]

#define DATE(stamp) [NSDate dateWithTimeIntervalSince1970:[stamp intValue]];




//设备屏幕尺寸

#define kScreen_Height   ([UIScreen mainScreen].bounds.size.height)

#define kScreen_Width    ([UIScreen mainScreen].bounds.size.width)

#define kScreen_Frame    (CGRectMake(0, 0 ,kScreen_Width,kScreen_Height))

#define kScreen_CenterX  kScreen_Width/2

#define kScreen_CenterY  kScreen_Height/2



//应用尺寸(不包括状态栏,通话时状态栏高度不是20,所以需要知道具体尺寸)

#define kContent_Height   ([UIScreen mainScreen].applicationFrame.size.height)

#define kContent_Width    ([UIScreen mainScreen].applicationFrame.size.width)

#define kContent_Frame    (CGRectMake(0, 0 ,kContent_Width,kContent_Height))

#define kContent_CenterX  kContent_Width/2

#define kContent_CenterY  kContent_Height/2




/*

类似九宫格的九个点

 

 p1 --- p2 --- p3

 |      |      |

 p4 --- p5 --- p6

 |      |      |

 p7 --- p8 --- p9

 

*/

#define kP1 CGPointMake(0                 ,0)

#define kP2 CGPointMake(kContent_Width/2  ,0)

#define kP3 CGPointMake(kContent_Width    ,0)

#define kP4 CGPointMake(0                 ,kContent_Height/2)

#define kP5 CGPointMake(kContent_Width/2  ,kContent_Height/2)

#define kP6 CGPointMake(kContent_Width    ,kContent_Height/2)

#define kP7 CGPointMake(0                 ,kContent_Height)

#define kP8 CGPointMake(kContent_Width/2  ,kContent_Height)

#define kP9 CGPointMake(kContent_Width    ,kContent_Height)


//*********************************************



//GCD

#define BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)

#define MAIN(block) dispatch_async(dispatch_get_main_queue(),block)



//Device

#define isIOS4 ([[[UIDevice currentDevice] systemVersion] intValue]==4)

#define isIOS5 ([[[UIDevice currentDevice] systemVersion] intValue]==5)

#define isIOS6 ([[[UIDevice currentDevice] systemVersion] intValue]==6)

#define isAfterIOS4 ([[[UIDevice currentDevice] systemVersion] intValue]>4)

#define isAfterIOS5 ([[[UIDevice currentDevice] systemVersion] intValue]>5)

#define isAfterIOS6 ([[[UIDevice currentDevice] systemVersion] intValue]>6)


#define iOS ([[[UIDevice currentDevice] systemVersion] floatValue])


#define isRetina ([[UIScreen mainScreen] scale]==2)

#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)

#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)



//拨打电话

#define canTel                 ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel:"]])

#define tel(phoneNumber)       ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNumber]]])

#define telprompt(phoneNumber) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt:%@",phoneNumber]]])


//打开URL

#define canOpenURL(appScheme) ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:appScheme]])

#define openURL(appScheme) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:appScheme]])


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值