写代码时,了解到设备的信息是有帮助的。 UIDevice 提供了关系系统设备的一些必须信息。可以参考之。
为了方便使用,对前进行一些扩展:
1. iOS系统版本
随着 iOS 系统的升级, API 有增删,应用需要兼容多个版本系统时需要考虑。下面是定义了相关的宏:
#define IOS6_OR_LATER ( [[[UIDevice currentDevice] systemVersion] compare:@"6.0"] != NSOrderedAscending )
#define IOS5_OR_LATER ( [[[UIDevice currentDevice] systemVersion] compare:@"5.0"] != NSOrderedAscending )
#define IOS4_OR_LATER ( [[[UIDevice currentDevice] systemVersion] compare:@"4.0"] != NSOrderedAscending )
#define IOS3_OR_LATER ( [[[UIDevice currentDevice] systemVersion] compare:@"3.0"] != NSOrderedAscending )
2. iOS 系统的版本,UIDevice 已经提供
+ (NSString *)osVersion
{
return [NSString stringWithFormat:@"%@ %@", [UIDevice currentDevice].systemName, [UIDevice currentDevice].systemVersion];
}
3. 应用版本信息
每个应用都有自己定义的一套递增的版本信息,记录在 *-info.plist 上
+ (NSString *)appVersion
{
return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
}
4. 设备和 UUID
前者 UIDevice 已经提供,用于判断是 ipad, iphone或者其它。 UUID 在 iOS6 前后有变化。另外还有相应的开源UUID包。
+ (NSString *)deviceModel
{
return [UIDevice currentDevice].model;
}
+ (NSString *)deviceUUID
{
if (IOS6_OR_LATER) {
return [[UIDevice currentDevice].identifierForVendor UUIDString];
}else{
return [UIDevice currentDevice].uniqueIdentifier;
}
}
5. 判断是否越狱
越狱,已经安装了一些相关的app,只要判断是否存在即可知道是否越狱。
static const char * __jb_app = NULL;
+ (BOOL)isJailBroken
{
static const char * __jb_apps[] =
{
"/Application/Cydia.app",
"/Application/limera1n.app",
"/Application/greenpois0n.app",
"/Application/blackra1n.app",
"/Application/blacksn0w.app",
"/Application/redsn0w.app",
NULL
};
__jb_app = NULL;
// method 1
for ( int i = 0; __jb_apps[i]; ++i )
{
if ( [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithUTF8String:__jb_apps[i]]] )
{
__jb_app = __jb_apps[i];
return YES;
}
}
// method 2
if ( [[NSFileManager defaultManager] fileExistsAtPath:@"/private/var/lib/apt/"] )
{
return YES;
}
// method 3
if ( 0 == system("ls") )
{
return YES;
}
return NO;
}
+ (NSString *)jailBreaker
{
if ( __jb_app )
{
return [NSString stringWithUTF8String:__jb_app];
}
else
{
return @"";
}
}