Swift UIAlertView正在使iOS 7上的应用程序崩溃

Swift UIAlertView正在使iOS 7上的应用程序崩溃,swift,uialertview,Swift,Uialertview,我刚刚在app Store上发布了我的应用程序,但我刚刚收到电子邮件,并被告知崩溃有问题 问题在于警报视图会在我的应用程序应该出现时崩溃(仅在iOS 7中)。我已经准备好了一些代码来解决这个问题,但是在iOS7的某些版本中似乎不起作用 这是我目前的代码: @IBAction func resetAllButton(sender : AnyObject) { //If statement to check whether the modern style alert is availabl

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

问题在于警报视图会在我的应用程序应该出现时崩溃(仅在iOS 7中)。我已经准备好了一些代码来解决这个问题,但是在iOS7的某些版本中似乎不起作用

这是我目前的代码:

@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上崩溃?

我在发布版本中也遇到了同样的问题。 这似乎是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解决方案:

//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)

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

您知道它在哪里/如何崩溃吗?您有stacktrace、行号或任何控制台输出吗?不幸的是没有。我没有iOS 7设备来测试它。它们似乎在iOS 7.1上,我可以在模拟器上测试,但在模拟器上却没有。我所知道的是,当任何UIAlertView显示时。而且它只在iOS 7上使用。我刚刚有一台iOS 7.1设备可以试用。当它运行从商店下载的应用程序时也会出现同样的问题,但是只要我通过我的Xcode项目运行它,一切都会很好地工作。现在你有了一台设备——你能通过Xcode获得崩溃报告吗?(1)获得详细信息、故障类型等的stacktrace,然后(2)根据你的应用程序对报告进行符号化,以确定崩溃的确切来源?以下是我问题的更新版本:我匆忙尝试修复问题,最终更改了调试生成设置,使其与分发设置相同,分发设置允许我在通过项目运行时重新创建崩溃。问题是我没有记录我改变了什么,所以事情变得相当混乱。太好了。他试图通过一个“官方”快速呼叫来解释为什么该应用程序在发布时崩溃。实现并修复了它。你有没有向苹果提交错误报告?还没有,我在开发者论坛上读了一些帖子,似乎他们知道这个问题,但我在论坛上看到的解决方案在我的案例中不起作用…好吧-我提交了一个错误,rdar://18478207. 希望他们能很快解决这个问题!很好的解决方案。我甚至没有想过只做ObjC中的检查部分。多谢各位+1.谢谢!请投票支持这一正确答案!:)