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

4

2 回答 2

8

那是您的应用程序的部署目标(可以安装您的应用程序的最低版本),而不是应用程序在设备中运行的版本。

在项目的设置中,您可以设置该字段:

在此处输入图像描述

如果你像这样改变它,这个输入:

NSLog(@"Version %i", __IPHONE_OS_VERSION_MIN_REQUIRED);

返回 7000

如果您想要检查操作系统的实际版本,我建议您参考这个问题:

如何查看iOS版本?

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

于 2013-09-20T12:58:15.530 回答
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

迅速:

    if #available(iOS 13, *) {
        toSearchBar?.isHidden = true
    } else {
        // a path way to discovering how fast UIKit will rot
        // now that there is SwiftUI
    }
于 2015-11-18T11:02:05.460 回答