Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

swift - UIAlertView is crashing app on iOS 7

I have just released my app on the App Store however I have just been emailed and told that there is issues with crashing.

The issue is with Alert Views which crash my app when they are supposed to appear (only in iOS 7). I had some code in place that was supposed to fix this issue, however it doesn't seem to work in certain versions of iOS 7.

Here is my current code:

@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()
    }
}

What can I do to ensure that my app doesn't crash on any version of iOS 7 or 8?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I had the same problem in the relese build. It seems an internal bug of the swift compiler (using Xcode 6.0.1 (6A317) )

I solved actually with a objC helper class with this method:

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

called in my swift code with a bridge header

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...