UIAlertView/UIAlertController iOS 7 and iOS 8 compatibility

后端 未结 14 1555
暗喜
暗喜 2020-11-28 22:31

I am using Swift to write an app and I need to show an alert. The app must be iOS 7 and iOS 8 compatible. Since UIAlertView has been replaced with UIAlert

14条回答
  •  醉酒成梦
    2020-11-28 22:51

    The detection pattern is identical to the Objective-C style.

    You need to detect whether the current active runtime has the ability to instantiate this class

    if objc_getClass("UIAlertController") != nil {
    
         println("UIAlertController can be instantiated")
    
          //make and use a UIAlertController
    
     }
     else {
    
          println("UIAlertController can NOT be instantiated")
    
          //make and use a UIAlertView
    }
    

    Don't try and work out this based on the OS version. You need to detect abilities NOT OS.

    EDIT

    The original detector for this answer NSClassFromString("UIAlertController") fails under -O optimisation so its been changed to the current version which does work for Release builds

    EDIT 2

    NSClassFromString is working at all optimisations in Xcode 6.3/Swift 1.2

提交回复
热议问题