我想检查用户是否在 iOS 5.0 以下版本上运行应用程序,并在应用程序中显示一个标签。
我该如何以编程方式检测用户设备上运行的 iOS 版本?
谢谢!
我想检查用户是否在 iOS 5.0 以下版本上运行应用程序,并在应用程序中显示一个标签。
我该如何以编程方式检测用户设备上运行的 iOS 版本?
谢谢!
最佳当前版本,无需在NSString中处理数字搜索,是定义宏(请参见原回答:检查iPhone iOS版本)。
这些宏在Github上存在,请参见:https://github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h
像这样:
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
并像这样使用它们:
if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {
// code here
}
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
// code here
}
获取操作系统版本:
[[UIDevice currentDevice] systemVersion]
返回字符串,可通过转换为 int/float
-[NSString floatValue]
-[NSString intValue]
由于类型的原因,这两个值(floatValue、intValue)都将被削减,5.0.1 将变成 5.0 或 5(浮点数或整数)。如果要精确比较它们,您需要将其分离为 INT 数组。请在此处检查已接受的答案:检查 iPhone iOS 版本
NSString *ver = [[UIDevice currentDevice] systemVersion];
int ver_int = [ver intValue];
float ver_float = [ver floatValue];
并像这样进行比较
NSLog(@"System Version is %@",[[UIDevice currentDevice] systemVersion]);
NSString *ver = [[UIDevice currentDevice] systemVersion];
float ver_float = [ver floatValue];
if (ver_float < 5.0) return false;
针对 Swift 4.0 语法
以下示例仅检查设备是否为 iOS11
或更高版本。
let systemVersion = UIDevice.current.systemVersion
if systemVersion.cgFloatValue >= 11.0 {
//"for ios 11"
}
else{
//"ios below 11")
}
从iOS 8开始,我们可以在NSProcessInfo
上使用新的isOperatingSystemAtLeastVersion
方法。
NSOperatingSystemVersion ios8_0_1 = (NSOperatingSystemVersion){8, 0, 1};
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:ios8_0_1]) {
// iOS 8.0.1 and above logic
} else {
// iOS 8.0.0 and below logic
}
请注意,这个API在iOS 7上并不存在,所以会导致崩溃。如果你要支持iOS 7及以下版本,可以使用以下代码进行检查:
if ([NSProcessInfo instancesRespondToSelector:@selector(isOperatingSystemAtLeastVersion:)]) {
// conditionally check for any version >= iOS 8 using 'isOperatingSystemAtLeastVersion'
} else {
// we're on iOS 7 or below
}
为了完整起见,这里提供了一种由苹果公司在 iOS 7 UI 过渡指南 中提出的替代方法,该方法涉及检查基础框架版本。
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
// Load resources for iOS 6.1 or earlier
} else {
// Load resources for iOS 7 or later
}
isOperatingSystemAtLeastVersion
将会触发一个未识别的实例异常选择器。 - edc1591我知道我回答这个问题已经太晚了。我不确定我的方法是否仍适用于低版本的iOS(<5.0):
NSString *platform = [UIDevice currentDevice].model;
NSLog(@"[UIDevice currentDevice].model: %@",platform);
NSLog(@"[UIDevice currentDevice].description: %@",[UIDevice currentDevice].description);
NSLog(@"[UIDevice currentDevice].localizedModel: %@",[UIDevice currentDevice].localizedModel);
NSLog(@"[UIDevice currentDevice].name: %@",[UIDevice currentDevice].name);
NSLog(@"[UIDevice currentDevice].systemVersion: %@",[UIDevice currentDevice].systemVersion);
NSLog(@"[UIDevice currentDevice].systemName: %@",[UIDevice currentDevice].systemName);
您可以获得以下结果:
[UIDevice currentDevice].model: iPhone
[UIDevice currentDevice].description: <UIDevice: 0x1cd75c70>
[UIDevice currentDevice].localizedModel: iPhone
[UIDevice currentDevice].name: Someones-iPhone002
[UIDevice currentDevice].systemVersion: 6.1.3
[UIDevice currentDevice].systemName: iPhone OS
[[UIDevice currentDevice] systemVersion]
[[UIDevice currentDevice] systemVersion];
或者像下面这样检查版本:
你可以从这里获取以下宏。
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(IOS_VERSION_3_2_0))
{
UIImageView *background = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cs_lines_back.png"]] autorelease];
theTableView.backgroundView = background;
}
希望这可以帮助到您
[[[UIDevice currentDevice] systemVersion] floatValue]
Marek Sebera的工具大多数时候都很好用,但如果你像我一样需要经常检查iOS版本,你就不想在内存中频繁运行宏,因为这会导致非常轻微的减速,特别是在旧设备上。
相反,你希望将iOS版本计算为一个浮点数并将其存储在某个地方。在我的情况下,我有一个GlobalVariables
单例类,我使用这个类来检查我的代码中的iOS版本,例如:
if ([GlobalVariables sharedVariables].iOSVersion >= 6.0f) {
// do something if iOS is 6.0 or greater
}
@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
[[UIDevice currentDevice] systemVersion]
NSString转换为CGFloat,这样就可以随时访问,而不使用本页上已经指出的任何不当方法。我的方法假定版本字符串采用n.nn.nn格式(允许后面的位数缺失),适用于iOS5+。在测试中,此方法比不断运行宏要快得多。UIDevice.CurrentDevice.SystemVersion.Split('.')[0]
对于小版本,请使用:
UIDevice.CurrentDevice.SystemVersion.Split('.')[1]
要获取更具体的版本号信息并将主要版本和次要版本分开:
```NSString* versionString = [UIDevice currentDevice].systemVersion;
NSArray* vN = [versionString componentsSeparatedByString:@"."];
数组vN
将包含主版本和次版本作为字符串,但如果您想进行比较,则应该将版本号存储为数字(整数)。您可以添加以下代码将它们存储在C数组* versionNumbers
中:
int versionNumbers[vN.count];
for (int i = 0; i < sizeof(versionNumbers)/sizeof(versionNumbers[0]); i++)
versionNumbers[i] = [[vN objectAtIndex:i] integerValue];
* 这里使用C数组是为了更简洁的语法。
if([[[UIDevice currentDevice] systemVersion] integerValue] < 5){
// do something
};