如何查看iOS版本?

Joh*_*ohn 837 objective-c ios

我想检查iOS设备的版本是否大于3.1.3 我尝试过的东西:

[[UIDevice currentDevice].systemVersion floatValue]
Run Code Online (Sandbox Code Playgroud)

但它不起作用,我只想要一个:

if (version > 3.1.3) { }
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

yas*_*urk 1052

/*
 *  System Versioning Preprocessor Macros
 */ 

#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)

/*
 *  Usage
 */ 

if (SYSTEM_VERSION_LESS_THAN(@"4.0")) {
    ...
}

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"3.1.1")) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

  • +1.将其保存在可以包含在需要的标题中是我能想到的最简单的解决方案.在某些情况下,这比使用NSClassFromString检查可用性更可靠.另一个例子是在iAd中,如果你在4.2之前的版本中使用ADBannerContentSizeIdentifierPortrait,你将获得EXEC_BAD_ACCESS,但在4.1之后不推荐使用等效的ADBannerContentSizeIdentifier320x50. (20认同)
  • 这对10s不起作用.例如,比较4.10和4.9会得到错误的结果. (7认同)
  • 在使用可选的`.0`数字时应该非常小心.例如`SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0.0")`在iOS 7.0上给出了错误的结果. (7认同)

Jus*_*tin 979

快速回答......


由于雨燕2.0的,你可以使用#available在一个ifguard保护应该只在某些系统上运行的代码.

if #available(iOS 9, *) {}


在Objective-C中,您需要检查系统版本并执行比较.

[[NSProcessInfo processInfo] operatingSystemVersion] 在iOS 8及更高版本中.

从Xcode 9开始:

if (@available(iOS 9, *)) {}


完整答案......

在Objective-C和Swift中,在极少数情况下,最好避免依赖操作系统版本作为设备或操作系统功能的指示.通常有一种更可靠的方法来检查特定要素或类是否可用.

检查是否存在API:

例如,您可以使用以下方法检查UIPopoverController当前设备上是否可用NSClassFromString:

if (NSClassFromString(@"UIPopoverController")) {
    // Do something
}
Run Code Online (Sandbox Code Playgroud)

对于弱链接类,直接向类消息是安全的.值得注意的是,这适用于未明确链接为"必需"的框架.对于缺少的类,表达式的计算结果为nil,条件不正确:

if ([LAContext class]) {
    // Do something
}
Run Code Online (Sandbox Code Playgroud)

某些类(如CLLocationManagerUIDevice)提供了检查设备功能的方法:

if ([CLLocationManager headingAvailable]) {
    // Do something
}
Run Code Online (Sandbox Code Playgroud)

检查是否存在符号:

偶尔,您必须检查是否存在常数.这在iOS 8中引入UIApplicationOpenSettingsURLString,用于加载设置应用程序-openURL:.在iOS 8之前该值不存在.将nil传递给此API将崩溃,因此您必须首先检查常量是否存在:

if (&UIApplicationOpenSettingsURLString != NULL) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
Run Code Online (Sandbox Code Playgroud)

与操作系统版本相比:

让我们假设您面临检查操作系统版本的相对罕见的需求.对于面向iOS 8及更高NSProcessInfo版本的项目,包括一种执行版本比较的方法,错误机会较少:

- (BOOL)isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version
Run Code Online (Sandbox Code Playgroud)

针对旧系统的项目可以使用systemVersionon UIDevice.Apple在其GLSprite示例代码中使用它.

// A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
// class is used as fallback when it isn't available.
NSString *reqSysVer = @"3.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) {
    displayLinkSupported = TRUE;
}
Run Code Online (Sandbox Code Playgroud)

如果由于某种原因您决定这systemVersion是您想要的,请确保将其视为字符串,否则您可能会截断修补程序修订号(例如3.1.2 - > 3.1).

  • 在某些特定情况下,需要检查系统版本.例如,私有n 3.x的几个类和方法在4.0中公开,所以如果你只是检查它们的可用性,你会在3.x中得到错误的结果.此外,UIScrollViews处理缩放比例的方式在3.2及更高版本中略有变化,因此您需要检查OS版本以便适当地处理结果. (161认同)
  • 你需要在这里小心一点,因为`[@"10.0"比较:@"10"选项:NSNumericSearch]`返回`NSOrderedDescending`,这可能完全没有意图.(我可能会期待'NSOrderedSame`.)这至少是理论上的可能性. (10认同)
  • iOS 3.2似乎没有在`UISplitViewController`中发送`-viewWillAppear`.我的黑客是确定是否<iOS 4.0,并在Root View的`-didSelectRowAtIndexPath`中将其发送到Detail View Controller. (2认同)
  • 随着TVos的引入,这甚至变得更加重要.请参阅使用9.1 SDK时TVos将返回9.0.所以检查类或方法(选择器)是最好的方法,在此之后你应该检查[`NSFoundationVersionNumber`](https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/ Miscellaneous/Foundation_Constants/index.html#// apple_ref/doc/constant_group/Foundation_Version_Number)并且只有在不可能的情况下才能检查`UIDevice`上的系统版本. (2认同)

Car*_*rlJ 252

正如建议苹果官方文档:您可以使用NSFoundationVersionNumber,从NSObjCRuntime.h头文件.

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
    // here you go with iOS 7
}
Run Code Online (Sandbox Code Playgroud)

  • +1这实际上是*最安全*和最准确的选项. (40认同)
  • iOS 5 SDK中不存在Um..`NSFoundationVersionNumber_iOS_6_1`. (8认同)
  • 不,但苹果会在iOS 9中添加```NSFoundationVersionNumber_iOS_8_0```,只需检查```NSFoundationVersionNumber> NSFoundationVersionNumber_iOS_7_1``` (7认同)
  • 如果有一天有iOS 6.2更新? (5认同)
  • 不,你错了,NSFoundationVersionNumber_iOS_7_0不存在.但是,我们可以使用`#ifndef NSFoundationVersionNumber_iOS_7_0 #define NSFoundationVersionNumber_iOS_7_0 1047.00 #endif`来定义它.为了避免这样的混乱,我选择了来自yasirmturk的SYSTEM_VERSION_LESS_THAN(v)宏 (3认同)
  • @Kjuly您可以自己定义缺少的版本号:https://github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h(查看102-130行) (2认同)
  • 两个 iOS 版本被赋予相同的 Foundation 版本号是很常见的。最近的例子:9.2 &amp; 9.3 都是 1242.12,8.3 &amp; 8.4 都是 1144.17。更喜欢 yasimturk 答案的另一个原因。 (2认同)

Cœu*_*œur 100

Objective-C中启动Xcode 9 :

if (@available(iOS 11, *)) {
    // iOS 11 (or newer) ObjC code
} else {
    // iOS 10 or older code
}
Run Code Online (Sandbox Code Playgroud)

Swift中启动Xcode 7 :

if #available(iOS 11, *) {
    // iOS 11 (or newer) Swift code
} else {
    // iOS 10 or older code
}
Run Code Online (Sandbox Code Playgroud)

对于版本,您可以指定MAJOR,MINOR或PATCH(有关定义,请参阅http://semver.org/).例子:

  • iOS 11并且iOS 11.0是相同的最小版本
  • iOS 10,iOS 10.3,iOS 10.3.1是不同的最小版本

您可以为任何这些系统输入值:

  • iOS,macOS,watchOS,tvOS

我的一个pod获取的真实案例:

if #available(iOS 10.0, tvOS 10.0, *) {
    // iOS 10+ and tvOS 10+ Swift code
} else {
    // iOS 9 and tvOS 9 older code
}
Run Code Online (Sandbox Code Playgroud)

文件


Tra*_* M. 44

这用于在Xcode中检查兼容的SDK版本,如果您有一个拥有不同Xcode版本的大型团队或支持共享相同代码的不同SDK的多个项目:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
  //programming in iOS 8+ SDK here
#else
  //programming in lower than iOS 8 here   
#endif
Run Code Online (Sandbox Code Playgroud)

你真正想要的是检查设备上的iOS版本.你可以这样做:

if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
  //older than iOS 8 code here
} else {
  //iOS 8 specific code here
}
Run Code Online (Sandbox Code Playgroud)

Swift版本:

if let version = Float(UIDevice.current.systemVersion), version < 9.3 {
    //add lower than 9.3 code here
} else {
    //add 9.3 and above code here
}
Run Code Online (Sandbox Code Playgroud)

当前版本的swift应该使用这个:

if #available(iOS 12, *) {
    //iOS 12 specific code here
} else {
    //older than iOS 12 code here
}
Run Code Online (Sandbox Code Playgroud)


Jon*_*pan 37

尝试:

NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"3.1.3" options: NSNumericSearch];
if (order == NSOrderedSame || order == NSOrderedDescending) {
    // OS version >= 3.1.3
} else {
    // OS version < 3.1.3
}
Run Code Online (Sandbox Code Playgroud)

  • 这样做可能会使逻辑变得不那么清晰.但是,操作应该是等效的. (3认同)
  • 为什么不在这里颠倒顺序并保存比较,因为@"3.1.3"和systemVersion都是NSStrings?即:`if([@"3.1.3"compare:[UIDevice currentDevice] .systemVersion options:NSNumericSearch] == NSOrderedDescending); // OS version> = 3.1.3 else; // OS version <3.1.3` (2认同)

Dan*_*sko 35

首选方法

在Swift 2.0中,Apple使用更方便的语法添加了可用性检查(在此处阅读更多内容).现在,您可以使用更清晰的语法检查操作系统版本:

if #available(iOS 9, *) {
    // Then we are on iOS 9
} else {
    // iOS 8 or earlier
}
Run Code Online (Sandbox Code Playgroud)

这是首选的检查respondsToSelector等(Swift中的新功能).现在,如果您没有正确地保护代码,编译器将始终警告您.


Pre Swift 2.0

iOS 8中的新功能NSProcessInfo允许更好的语义版本检查.

在iOS 8及更高版本上部署

对于iOS 8.0或更高版本的最低部署目标,请使用NSProcessInfo operatingSystemVersionisOperatingSystemAtLeastVersion.

这将产生以下结果:

let minimumVersion = NSOperatingSystemVersion(majorVersion: 8, minorVersion: 1, patchVersion: 2)
if NSProcessInfo().isOperatingSystemAtLeastVersion(minimumVersion) {
    //current version is >= (8.1.2)
} else {
    //current version is < (8.1.2)
}
Run Code Online (Sandbox Code Playgroud)

在iOS 7上部署

对于iOS 7.1或更低版本的最低部署目标,请使用与NSStringCompareOptions.NumericSearchon 进行比较 UIDevice systemVersion.

这会产生:

let minimumVersionString = "3.1.3"
let versionComparison = UIDevice.currentDevice().systemVersion.compare(minimumVersionString, options: .NumericSearch)
switch versionComparison {
    case .OrderedSame, .OrderedDescending:
        //current version is >= (3.1.3)
        break
    case .OrderedAscending:
        //current version is < (3.1.3)
        fallthrough
    default:
        break;
}
Run Code Online (Sandbox Code Playgroud)

更多阅读NSHipster.


Seg*_*gev 9

我总是将它们保存在我的Constants.h文件中:

#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES) 
#define IS_OS_5_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0)
#define IS_OS_6_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
#define IS_OS_7_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
#define IS_OS_8_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
Run Code Online (Sandbox Code Playgroud)


Jef*_*Jef 6

+(BOOL)doesSystemVersionMeetRequirement:(NSString *)minRequirement{

// eg  NSString *reqSysVer = @"4.0";


  NSString *currSysVer = [[UIDevice currentDevice] systemVersion];

  if ([currSysVer compare:minRequirement options:NSNumericSearch] != NSOrderedAscending)
  {
    return YES;
  }else{
    return NO;
  }


}
Run Code Online (Sandbox Code Playgroud)


Tak*_*aki 6

随着版本包含在类NV-IOS版本项目(Apache许可证2.0版),很容易得到和比较的iOS版本.下面的示例代码转储iOS版本并检查版本是否大于或等于6.0.

// Get the system version of iOS at runtime.
NSString *versionString = [[UIDevice currentDevice] systemVersion];

// Convert the version string to a Version instance.
Version *version = [Version versionWithString:versionString];

// Dump the major, minor and micro version numbers.
NSLog(@"version = [%d, %d, %d]",
    version.major, version.minor, version.micro);

// Check whether the version is greater than or equal to 6.0.
if ([version isGreaterThanOrEqualToMajor:6 minor:0])
{
    // The iOS version is greater than or equal to 6.0.
}

// Another way to check whether iOS version is
// greater than or equal to 6.0.
if (6 <= version.major)
{
    // The iOS version is greater than or equal to 6.0.
}
Run Code Online (Sandbox Code Playgroud)

项目页面: nv-ios-version
TakahikoKawasaki/nv-ios-version

博客:在运行时
获取和比较iOS版本与Version类在运行时获取并比较iOS版本与Version类


bca*_*tle 6

我知道这是一个老问题,但有人应该提到Availability.h. 这里的所有其他方法都是运行时解决方案,并且不适用于头文件、类类别或 ivar 定义。

对于这些情况,请使用

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0 && defined(__IPHONE_14_0)
  // iOS 14+ code here
#else
  // Pre iOS 14 code here
#endif
Run Code Online (Sandbox Code Playgroud)

h/t这个答案


iGo*_*iGo 5

使用swift Forget [[UIDevice currentDevice] systemVersion]和NSFoundationVersionNumber检查系统版本的新方法.

我们可以使用NSProcessInfo -isOperatingSystemAtLeastVersion

     import Foundation

     let yosemite = NSOperatingSystemVersion(majorVersion: 10, minorVersion: 10, patchVersion: 0)
     NSProcessInfo().isOperatingSystemAtLeastVersion(yosemite) // false
Run Code Online (Sandbox Code Playgroud)


Oli*_*ain 5

的UIDevice + IOSVersion.h

@interface UIDevice (IOSVersion)

+ (BOOL)isCurrentIOSVersionEqualToVersion:(NSString *)iOSVersion;
+ (BOOL)isCurrentIOSVersionGreaterThanVersion:(NSString *)iOSVersion;
+ (BOOL)isCurrentIOSVersionGreaterThanOrEqualToVersion:(NSString *)iOSVersion;
+ (BOOL)isCurrentIOSVersionLessThanVersion:(NSString *)iOSVersion;
+ (BOOL)isCurrentIOSVersionLessThanOrEqualToVersion:(NSString *)iOSVersion

@end
Run Code Online (Sandbox Code Playgroud)

的UIDevice + IOSVersion.m

#import "UIDevice+IOSVersion.h"

@implementation UIDevice (IOSVersion)

+ (BOOL)isCurrentIOSVersionEqualToVersion:(NSString *)iOSVersion
{
    return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] == NSOrderedSame;
}

+ (BOOL)isCurrentIOSVersionGreaterThanVersion:(NSString *)iOSVersion
{
    return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] == NSOrderedDescending;
}

+ (BOOL)isCurrentIOSVersionGreaterThanOrEqualToVersion:(NSString *)iOSVersion
{
    return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] != NSOrderedAscending;
}

+ (BOOL)isCurrentIOSVersionLessThanVersion:(NSString *)iOSVersion
{
    return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] == NSOrderedAscending;
}

+ (BOOL)isCurrentIOSVersionLessThanOrEqualToVersion:(NSString *)iOSVersion
{
    return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] != NSOrderedDescending;
}

@end
Run Code Online (Sandbox Code Playgroud)