【问题标题】:UIAlertView is crashing app on iOS 7UIAlertView 在 iOS 7 上导致应用程序崩溃
【发布时间】:2014-09-18 19:58:20
【问题描述】:

我刚刚在 App Store 上发布了我的应用程序,但是我刚刚收到电子邮件并告知我存在崩溃问题。

问题在于警报视图在我的应用程序应该出现时崩溃(仅在 iOS 7 中)。我有一些代码可以解决这个问题,但它似乎不适用于某些 iOS 7 版本。

这是我当前的代码:

@IBAction func resetAllButton(sender : AnyObject) {
    //If statement to check whether the modern style alert is available. Prior to iOS 8 it is not.
    if let gotModernAlert: AnyClass = NSClassFromString("UIAlertController") {

        println("UIAlertController can be instantiated")

        var alert = UIAlertController(title: "Start Over", message: "Are you sure you want to start over? This will erase your budget and all transactions.", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "I'm sure!", style: UIAlertActionStyle.Default, handler:{ (ACTION :UIAlertAction!)in
            self.resetView()
        }))
        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))

        self.presentViewController(alert, animated: true, completion: nil)
    }
    else {

        println("UIAlertController can NOT be instantiated")

        var alertView = UIAlertView()
        alertView.delegate = self
        alertView.title = "Start Over"
        alertView.message = "Are you sure you want to start over? This will erase your budget and all transactions."
        alertView.addButtonWithTitle("I'm sure!")
        alertView.addButtonWithTitle("Cancel")
        alertView.show()
    }
}

如何确保我的应用不会在任何版本的 iOS 7 或 8 上崩溃?

【问题讨论】:

  • 你知道它在哪里/如何崩溃吗?你有堆栈跟踪、行号或任何控制台输出吗?
  • 很遗憾没有。我没有 iOS 7 设备来测试它。它们似乎在 iOS 7.1 上,我显然可以在模拟器上进行测试,但它不会在模拟器上发生。我所知道的是,它是显示任何 UIAlertView 的时候。而且它只在 iOS 7 上。
  • 我刚刚有一个 iOS 7.1 设备可以试一试。它在运行从商店下载的应用程序时遇到了同样的问题,但是一旦我通过我的 Xcode 项目运行它,一切正常。
  • 现在你有了一个设备——你能不能通过 Xcode 获得崩溃报告,并且 (1) 获得包含详细信息、故障类型等的堆栈跟踪,然后 (2) 将报告与你的应用程序来确定崩溃的确切位置?
  • 这是我的问题的更新版本:stackoverflow.com/questions/25922180/… 我急忙尝试解决问题,最终将调试构建设置更改为与允许我重新创建的分发设置相同通过我的项目运行时崩溃。问题是我没有跟踪我所做的更改,所以事情变得非常混乱。

标签: swift uialertview


【解决方案1】:

我在发布版本中遇到了同样的问题。 这似乎是 swift 编译器的内部错误(使用 Xcode 6.0.1 (6A317))

我用这个方法实际解决了一个 objC 辅助类:

+ (BOOL) checkIfClassExists:(NSString *)className {
    id c = objc_getClass([className cStringUsingEncoding:NSASCIIStringEncoding]);
    if (c != nil) {
        return YES;
    } else {
        return NO;
    }
}

在我的 swift 代码中调用了一个桥头文件

if ObjCFix.checkIfClassExists("UIAlertController") {
    //iOS 8 code here
} else {
    //iOS 7 code here
}

【讨论】:

  • 完美。试图破译为什么应用程序在发布版本和“官方”Swift 调用时崩溃。实施并修复它。您是否向 Apple 提交了错误报告?
  • 还没有,我在开发者论坛上阅读了一些帖子,似乎他们知道这个问题,但我在论坛上看到的解决方案对我的情况不起作用......
  • 好的 - 我提交了一个错误,rdar://18478207。希望他们能很快搞定!
  • 很好的解决方案。我什至没想过只在 ObjC 中做检查部分。谢谢你。 +1。
  • 谢谢!请投票给这个作为正确答案! :)
【解决方案2】:

这是我的拖放快速解决方案:

//Alerts change in iOS8, this method is to cover iOS7 devices
func CozAlert(title: String, message: String, action: String, sender: UIViewController){

    if respondsToSelector("UIAlertController"){
        var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: action, style: UIAlertActionStyle.Default, handler:nil))
        sender.presentViewController(alert, animated: true, completion: nil)
    }
    else {
        var alert = UIAlertView(title: title, message: message, delegate: sender, cancelButtonTitle:action)
        alert.show()
    }
}

这样调用:

CozAlert("reportTitle", message: "reportText", action: "reportButton", sender: self)

请注意,这仅适用于最基本的警报,您可能需要额外的代码来处理高级内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多