How to check iOS version?

后端 未结 30 2517
一生所求
一生所求 2020-11-21 06:37

I want to check if the iOS version of the device is greater than 3.1.3 I tried things like:

[[UIDevice currentDevice].systemVersion         


        
30条回答
  •  我寻月下人不归
    2020-11-21 07:12

    Solution for checking iOS version in Swift

    switch (UIDevice.currentDevice().systemVersion.compare("8.0.0", options: NSStringCompareOptions.NumericSearch)) {
        case .OrderedAscending:
           println("iOS < 8.0")
    
        case .OrderedSame, .OrderedDescending:
           println("iOS >= 8.0")
    }
    

    Con of this solution: it is simply bad practice to check against OS version numbers, whichever way you do it. One should never hard code dependencies in this way, always check for features, capabilities or the existence of a class. Consider this; Apple may release a backwards compatible version of a class, if they did then the code you suggest would never use it as your logic looks for an OS version number and NOT the existence of the class.

    (Source of this information)

    Solution for checking the class existence in Swift

    if (objc_getClass("UIAlertController") == nil) {
       // iOS 7
    } else {
       // iOS 8+
    }
    

    Do not use if (NSClassFromString("UIAlertController") == nil) because it works correctly on the iOS simulator using iOS 7.1 and 8.2, but if you test on a real device using iOS 7.1, you will unfortunately notice that you will never pass through the else part of the code snippet.

提交回复
热议问题