0

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?

user3746428
  • 11,047
  • 20
  • 81
  • 137
  • Do you know where/how it's crashing? Do you have a stacktrace, line number, or any console output? – Craig Otis Sep 18 '14 at 19:59
  • Unfortunately not. I don't have an iOS 7 device to test it on. They seem to be on iOS 7.1 which I can obviously test on the simulator but it doesn't happen on the simulator. All I know is that it is when any UIAlertView is shown. And it's only on iOS 7. – user3746428 Sep 18 '14 at 20:13
  • I've just got an iOS 7.1 device to try it on. It's having the same issue when it's running the app downloaded from the store, however as soon as I run it via my Xcode project, everything works perfectly. – user3746428 Sep 18 '14 at 20:29
  • Now that you have a device though - can you get the crash report through Xcode and (1) get a stacktrace with details, type of failure, etc. and then (2) symbolicate the report against your app to figure out exactly where the crash originates? – Craig Otis Sep 18 '14 at 21:09
  • Here is an updated version of my question: http://stackoverflow.com/questions/25922180/app-crashing-due-to-uialertview-on-ios-7 I rushed into trying to fix the issue and ended up changing the debug build settings to be the same as the distribution settings which allowed me to recreate the crash when running via my project. The problem is that I didn't keep track of what I changed so things have become pretty confusing. – user3746428 Sep 18 '14 at 21:56

2 Answers2

3

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
}
The Elter
  • 235
  • 1
  • 9
0

Here is my drag and drop swift solution:

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

Call like this:

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

Beware this is only for the most basic alerts, you might need additional code for advanced stuff.

Esqarrouth
  • 38,543
  • 21
  • 161
  • 168