UIAlertView / UIAlertController兼容iOS 7和iOS 8 [英] UIAlertView/UIAlertController iOS 7 and iOS 8 compatibility

查看:169
本文介绍了UIAlertView / UIAlertController兼容iOS 7和iOS 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift编写应用程序,我需要显示警报。该应用必须兼容iOS 7和iOS 8。由于 UIAlertView 已替换为 UIAlertController ,如何检查 UIAlertController 可用而不检查系统版本?我听说Apple建议我们不要检查设备的系统版本,以确定API的可用性。

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 UIAlertController, how can I check if the UIAlertController is available without checking the system version? I have been hearing that Apple recommends that we should not check the system version of the device in order to determine the availability of an API.

这就是我用来做的iOS 8但在iOS 7上崩溃, dyld:未找到符号:_OBJC_CLASS _ $ _ UIAlertAction

This is what I am using for iOS 8 but this crashes on iOS 7 with "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)

如果我使用UIAlertView for iOS 8,我收到此警告:警告:尝试从视图控制器中解除< _UIAlertShimPresentingViewController:0x7bf72d60>正在进行演示或解雇!

If I use the UIAlertView for iOS 8, I get this warning: Warning: Attempt to dismiss from view controller <_UIAlertShimPresentingViewController: 0x7bf72d60> while a presentation or dismiss is in progress!

推荐答案

检测模式与目标相同C风格。

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.

编辑

此答案的原始检测器 NSClassFromString(UIAlertController) -O 优化下失败,因此已更改当前版本适用于发布版本

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

编辑2

NSClassFromString 正在Xcode 6.3 / Swift 1.2的所有优化中工作

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

这篇关于UIAlertView / UIAlertController兼容iOS 7和iOS 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆