【问题标题】:iOS: Preprocessor for OS version checkiOS:操作系统版本检查的预处理器
【发布时间】:2013-09-25 20:52:27
【问题描述】:

过去我使用以下预处理器代码有条件地执行不同 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

【问题讨论】:

    标签: ios objective-c c-preprocessor


    【解决方案1】:

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

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

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

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

    返回 7000

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

    How to check iOS version?

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

    【讨论】:

    【解决方案2】:
      #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
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-21
      • 2012-02-05
      • 2014-08-21
      • 1970-01-01
      相关资源
      最近更新 更多