iOS:用于检查操作系统版本的预处理器

5

过去,我使用以下预处理器代码来有条件地执行适用于不同iOS版本的代码:

#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
// target is iOS
    #if __IPHONE_OS_VERSION_MIN_REQUIRED < 60000
    // target is lower than iOS 6.0
    #else
    // target is at least iOS 6.0
    #endif
#endif

然而,在iOS 7中我遇到了以下问题:
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
// target is iOS
    #if __IPHONE_OS_VERSION_MIN_REQUIRED < 70000
    // target is lower than iOS 7.0
    NSLog(@"This message should only appear if iOS version is 6.x or lower");
    #else
    // target is at least iOS 7.0
    #endif
#endif

上面的NSLog信息会在iOS 7下的控制台上显示,我有做错什么吗?
编辑:以下代码在iOS 7(模拟器和设备)上运行。
NSLog(@"Version %i", __IPHONE_OS_VERSION_MIN_REQUIRED);

提示:版本号为60000

2个回答

8
这是您的应用程序的部署目标(应用程序可以安装的最低版本),而不是应用程序在设备上运行的版本。
在您的项目设置中,您可以设置该字段: enter image description here 如果您将其更改为以下内容,则输入如下:
NSLog(@"Version %i", __IPHONE_OS_VERSION_MIN_REQUIRED);

返回 7000

如果您想检查操作系统的实际版本,则可以参考此问题:

如何检查iOS版本?

但是,这是在运行时完成的,而不是在编译时完成的。


谢谢。这也是您链接的帖子的一个很好的补充:https://dev59.com/qmsz5IYBdhLWcg3wg4Gv#7848772 - FrankZp

1
  #ifdef __AVAILABILITY_INTERNAL__IPHONE_9_0_DEP__IPHONE_9_0
    // you're in Xcode 7.x and can use buggy SDK with ios 9.0 only functionality
        CGFloat iOSVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (iOSVersion>=9) {
        // same good old API that predates brave new post Steve Jobs world of bugs and crashes
    }
  #else
        // you're running Xcode 6.4 or older and should use older API here
  #endif

Swift:

    if #available(iOS 13, *) {
        toSearchBar?.isHidden = true
    } else {
        // a path way to discovering how fast UIKit will rot
        // now that there is SwiftUI
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接