iOS获取iPhone系统等信息和服务器返回空的异常处理

时间:2021-04-19 15:52:17

前言:

在项目中经常会遇到需要获取系统的信息来处理一些特殊的需求和服务端返回为空的处理,写在这里只是笔记一下。

获取设备的信息

NSLog(@"globallyUniqueString=%@",[[NSProcessInfo processInfo] globallyUniqueString]);//全球唯一标识
NSLog(@"uniqueIdentifie=%@",[UIDevice currentDevice].uniqueIdentifier);//唯一的标识 可用于区分设备
NSLog(@"name=%@",[UIDevice currentDevice].name);//设备的名称 谁的iPad
NSLog(@"systemName=%@",[UIDevice currentDevice].systemName);//系统的名称 iPhone OS
NSLog(@"systemVersion=%@",[UIDevice currentDevice].systemVersion);//设备系统的版本号
NSLog(@"model=%@",[UIDevice currentDevice].model);//设备的型号 iPad、iphone、ipod touch。。。
NSLog(@"localizedModel=%@",[UIDevice currentDevice].localizedModel);//本地化的模型 iPad
NSLog(@"batteryLevel=%lf",[UIDevice currentDevice].batteryLevel);//电池电量

获取手机型号和系统号

/**获取手机型号和系统号*/
+ (NSString*)getDeviceInfo
{ NSString *phoneVersion = [[UIDevice currentDevice] systemVersion];
NSString *deviceInfo = [NSString stringWithFormat:@"%@;%@",[self getPhoneDevice],phoneVersion]; return deviceInfo;
}

获取iPhone和iPad的机型

+ (NSString *)getPhoneDevice{
// 需要#import "sys/utsname.h"
struct utsname systemInfo;
uname(&systemInfo);
NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
NSLog(@"型号:%@", deviceString);
//iPhone
if ([deviceString isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
if ([deviceString isEqualToString:@"iPhone3,2"]) return @"Verizon iPhone 4";
if ([deviceString isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
if ([deviceString isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
if ([deviceString isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
if ([deviceString isEqualToString:@"iPhone5,3"]) return @"iPhone 5C";
if ([deviceString isEqualToString:@"iPhone5,4"]) return @"iPhone 5C";
if ([deviceString isEqualToString:@"iPhone6,1"]) return @"iPhone 5S";
if ([deviceString isEqualToString:@"iPhone6,2"]) return @"iPhone 5S";
if ([deviceString isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
if ([deviceString isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
if ([deviceString isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
if ([deviceString isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
if ([deviceString isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
if ([deviceString isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
if ([deviceString isEqualToString:@"iPhone9,3"]) return @"iPhone 7";
if ([deviceString isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
if ([deviceString isEqualToString:@"iPhone9,4"]) return @"iPhone 7 Plus"; //iPad
if ([deviceString isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
if ([deviceString isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
if ([deviceString isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
if ([deviceString isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
if ([deviceString isEqualToString:@"iPad1,1"]) return @"iPad";
if ([deviceString isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)";
if ([deviceString isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)";
if ([deviceString isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)";
if ([deviceString isEqualToString:@"i386"]) return @"Simulator";
if ([deviceString isEqualToString:@"x86_64"]) return @"Simulator";
return deviceString;
}

获取屏幕尺寸

//ios版本号
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
//是否为iOS 7.0以上
#define IS_IOS7 [[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0 #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) #define IS_IPHONE_4_OR_LESS ([UIScreen mainScreen].bounds.size.height < 568.0)
#define IS_IPHONE_5 ([UIScreen mainScreen].bounds.size.height == 568.0)
#define IS_IPHONE_6 ([UIScreen mainScreen].bounds.size.height == 667.0)
#define IS_IPHONE_6P ([UIScreen mainScreen].bounds.size.height == 736.0)
#define IS_IPHONE_6_OR_LATER ([UIScreen mainScreen].bounds.size.height >= 667.0)

空的处理方式

//判断服务器返回为空
#define checkIsNULL(__X__) (__X__) == [NSNull null] || (__X__) == nil ? @"" : [NSString stringWithFormat:@"%@", (__X__)]
------------------------------------------------------- //字符串是否为空
#define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO ) //数组是否为空
#define kArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0) //字典是否为空
#define kDictIsEmpty(dic) (dic.count == 0 || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0) //是否是空对象
#define kObjectIsEmpty(_object) (_object == nil \
|| [_object isKindOfClass:[NSNull class]] \
|| ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) \
|| ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))

防止循环引用

//弱引用/强引用
#define XLJWeakSelf(type)  __weak typeof(type) weak##type = type;
#define XLJStrongSelf(type)  __strong typeof(type) type = weak##type;
如果如哪里写的有问题,请及时issues me,以便我能更快的进行更新修复。
Email: marlonxlj@163.com