我正在使用Swift编写应用程序,并需要显示一个警告。该应用程序必须兼容iOS 7和iOS 8。由于UIAlertView
已经被UIAlertController
代替,那么如何在不检查系统版本的情况下检查UIAlertController
是否可用?我听说过Apple建议我们不要检查设备的系统版本来确定API的可用性。
这是我在iOS 8上使用的代码,但在iOS 7上会崩溃并显示“dyld: Symbol not found: _OBJC_CLASS_$_UIAlertAction
”:
let alert = UIAlertController(title: "Error", message: message, preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alert.addAction(cancelAction)
presentViewController(alert, animated: true, completion: nil)
如果我在iOS 8上使用UIAlertView,会收到此警告:警告:尝试从视图控制器<_UIAlertShimPresentingViewController: 0x7bf72d60>中解除显示,而此时正在进行显示或解除显示!
UIAlertView
大约5年左右。对于大多数应用程序来说,现在还不是切换到UIAlertController
的时候。如果可能的话,苹果希望开发人员与iOS 7保持向后兼容性,而iOS 7没有UIAlertController。我不确定为什么我的评论会被踩,因为我只是陈述了一个事实。 - Abhi Beckert