如何确定 iphone / ipad / ipod / appletv 是不是较旧、较新或等于同系列的其他设备?
Posted
技术标签:
【中文标题】如何确定 iphone / ipad / ipod / appletv 是不是较旧、较新或等于同系列的其他设备?【英文标题】:How to determine if iphone / ipad / ipod / appletv is older, newer or equal to other device of same family?如何确定 iphone / ipad / ipod / appletv 是否较旧、较新或等于同系列的其他设备? 【发布时间】:2013-01-18 12:14:23 【问题描述】:我如何比较不同的平台?
示例: 我想知道当前设备是否比 iPhone 4S 旧。
【问题讨论】:
如果您对我的问题投反对票,请发表评论。为了改进它,我需要知道为什么这是一个糟糕的问题。 【参考方案1】:将这些方法添加到某个类(例如 YourClass):
+(NSString*)deviceVersion
// get hardware device version string
struct utsname systemInfo;
uname(&systemInfo);
return [NSString stringWithCString:systemInfo.machine
encoding:NSUTF8StringEncoding];
/* possible values:
@"i386" (or similar) on the simulator //
@"iPod1,1" on iPod Touch
@"iPod2,1" on iPod Touch Second Generation
@"iPod3,1" on iPod Touch Third Generation
@"iPod4,1" on iPod Touch Fourth Generation
@"iPhone1,1" on iPhone
@"iPhone1,2" on iPhone 3G
@"iPhone2,1" on iPhone 3GS
@"iPad1,1" on iPad
@"iPad2,1" on iPad 2
@"iPad3,1" on iPad 3 (aka new iPad)
@"iPhone3,1" on iPhone 4
@"iPhone4,1" on iPhone 4S
@"iPhone5,1" on iPhone 5
@"iPhone5,2" on iPhone 5
...future version...? */
+(NSNumber *)getVersionForDevice:(NSString*)deviceToCheck
NSNumber* versionNumber = nil;
if (deviceToCheck && ![deviceToCheck isEqualToString:@""])
NSString* deviceMachine = [Utils deviceVersion];
NSString* deviceVersion = nil;
if ([deviceMachine hasPrefix:deviceToCheck])
deviceVersion = [deviceMachine stringByReplacingOccurrencesOfString:deviceToCheck withString:@""];
deviceVersion = [deviceVersion stringByReplacingOccurrencesOfString:@"," withString:@"."];
float floatVersion = [deviceVersion floatValue];
versionNumber = [NSNumber numberWithFloat:floatVersion];
return versionNumber;
+(BOOL)isSimulator
NSString* deviceMachine = [Utils deviceVersion];
// NSLog(@"Utils deviceMachine: %@", deviceMachine);
if (![deviceMachine hasPrefix:@"iPhone"] && ![deviceMachine hasPrefix:@"iPod"] && ![deviceMachine hasPrefix:@"iPad"])
return 1;
return 0;
那么,如果在 iPhone 上并想测试版本:
float iPhoneVersion = [[YourClass getVersionForDevice:@"iPhone"] floatValue];
为 iPad 做类似的调用... iPod...
【讨论】:
有趣!看起来很有希望【参考方案2】:如果你需要做比较,你可以使用
https://github.com/fossli/uidevice-extension/blob/master/Source/UIDevice-Hardware.h https://github.com/fossli/uidevice-extension/blob/master/Source/UIDevice-Hardware.m然后就可以轻松比较同类型的平台了
UIDeviceComparisonResult result = [[UIDevice currentDevice] compareCurrentPlatformWithOtherPlatform:@"iPhone4,1"];
BOOL isOlderThanIPhone4S = result == UIDeviceComparisonResultOtherNewer;
return isOlderThanIPhone4S;
所有平台的名称都可以在 UIDevice-Hardware.m 中找到
【讨论】:
以上是关于如何确定 iphone / ipad / ipod / appletv 是不是较旧、较新或等于同系列的其他设备?的主要内容,如果未能解决你的问题,请参考以下文章
阻止除 iPhone、iPod 和 iPAD 以外的移动设备和台式机
如何在 JavaScript 中区分 iPhone、iPad 和 iPod?
重定向如果在 Iphone/Ipod 上 Ipad/Ipad Mini 怎么样
如何使用Xilisoft iPad Magic Platinum for Mac从/向 iPad/iPod/iPhone 传输文件?