我想检查用户是否在iOS 5.0以下运行应用程序,并在应用程序中显示一个标签。
我如何以编程方式检测哪个iOS正在用户的设备上运行?
谢谢!
我想检查用户是否在iOS 5.0以下运行应用程序,并在应用程序中显示一个标签。
我如何以编程方式检测哪个iOS正在用户的设备上运行?
谢谢!
当前回答
在MonoTouch:
要获得Major版本,请使用:
UIDevice.CurrentDevice.SystemVersion.Split('.')[0]
对于小版本使用:
UIDevice.CurrentDevice.SystemVersion.Split('.')[1]
其他回答
在MonoTouch:
要获得Major版本,请使用:
UIDevice.CurrentDevice.SystemVersion.Split('.')[0]
对于小版本使用:
UIDevice.CurrentDevice.SystemVersion.Split('.')[1]
[[[UIDevice currentDevice] systemVersion] floatValue]
要获得分隔主版本和次版本的更具体版本号信息:
NSString* versionString = [UIDevice currentDevice].systemVersion;
NSArray* vN = [versionString componentsSeparatedByString:@"."];
数组vN将以字符串形式包含主版本和次版本,但如果您想进行比较,版本号应该存储为数字(int)。你可以添加这段代码将它们存储在c数组* versionNumbers中:
int versionNumbers[vN.count];
for (int i = 0; i < sizeof(versionNumbers)/sizeof(versionNumbers[0]); i++)
versionNumbers[i] = [[vN objectAtIndex:i] integerValue];
*这里使用的c数组更简洁的语法。
一个简单的检查iOS版本小于5(所有版本):
if([[[UIDevice currentDevice] systemVersion] integerValue] < 5){
// do something
};
Marek Sebera的大多数时候都很棒,但如果你像我一样,发现自己需要频繁地检查iOS版本,你就不想一直在内存中运行宏,因为你会遇到非常轻微的减速,尤其是在旧设备上。
相反,你希望将iOS版本作为浮点数计算一次,并将它存储在某个地方。在我的例子中,我有一个GlobalVariables单例类,我使用这样的代码来检查我的代码中的iOS版本:
if ([GlobalVariables sharedVariables].iOSVersion >= 6.0f) {
// do something if iOS is 6.0 or greater
}
要在应用程序中启用此功能,请使用以下代码(适用于使用ARC的iOS 5+):
GlobalVariables.h:
@interface GlobalVariables : NSObject
@property (nonatomic) CGFloat iOSVersion;
+ (GlobalVariables *)sharedVariables;
@end
GlobalVariables.m:
@implementation GlobalVariables
@synthesize iOSVersion;
+ (GlobalVariables *)sharedVariables {
// set up the global variables as a static object
static GlobalVariables *globalVariables = nil;
// check if global variables exist
if (globalVariables == nil) {
// if no, create the global variables class
globalVariables = [[GlobalVariables alloc] init];
// get system version
NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
// separate system version by periods
NSArray *systemVersionComponents = [systemVersion componentsSeparatedByString:@"."];
// set ios version
globalVariables.iOSVersion = [[NSString stringWithFormat:@"%01d.%02d%02d", \
systemVersionComponents.count < 1 ? 0 : \
[[systemVersionComponents objectAtIndex:0] integerValue], \
systemVersionComponents.count < 2 ? 0 : \
[[systemVersionComponents objectAtIndex:1] integerValue], \
systemVersionComponents.count < 3 ? 0 : \
[[systemVersionComponents objectAtIndex:2] integerValue] \
] floatValue];
}
// return singleton instance
return globalVariables;
}
@end
Now you're able to easily check the iOS version without running macros constantly. Note in particular how I converted the [[UIDevice currentDevice] systemVersion] NSString to a CGFloat that is constantly accessible without using any of the improper methods many have already pointed out on this page. My approach assumes the version string is in the format n.nn.nn (allowing for later bits to be missing) and works for iOS5+. In testing, this approach runs much faster than constantly running the macro.
希望这能帮助任何经历我的问题!