Uialertview/Uialertcontroller iOS 7 and iOS 8 Compatibility

UIAlertView/UIAlertController iOS 7 and iOS 8 compatibility

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.

EDIT

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

EDIT 2

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

UIAlertController if iOS 8, otherwise UIAlertView

Please see the answer of Erwan (below my answer) as I see it is the best.

--

You can check the iOS version to use appropriate control like this:

if (([[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] == NSOrderedAscending)) {
// use UIAlertView
}
else {
// use UIAlertController
}

Alert that can work on iOS 7 and iOS 8


  • Explicitly add UIKit in the "Link Binary With Libraries" section of your project's build phases.

  • You can test for the existence of UIAlertController like this:

    if NSClassFromString("UIAlertController") != nil {
    // Use it
    } else {
    // Fall back
    }
  • I wrote a wrapper that works on both iOS 7 and iOS 8. You can find it here. It takes a view controller followed by a bunch of optional arguments and any number of buttons:

    showAlert(self, style: .ActionSheet, sourceView: cell, completion: {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    },
    (.Default, "Send clipboard", {
    if someCondition {
    // completion must be specified because of a Swift bug (rdar://18041904)
    showAlert(self, title: "Nothing to send", message: "The clipboard is empty.", completion: nil,
    (.Cancel, "OK", nil)
    )
    }
    }),
    (.Cancel, "Cancel", nil)
    )

Change UIAlertView to UIAlertController of app to get updated to iOS 8

Nothing is "necessary", but for best compatibility going both backwards into the past and forwards into the future it is obviously best to adopt the new technology in iOS 8 and abandon the deprecated technology. So if you want your app to work both in iOS 7 and iOS 8, the broadest possible coverage would require that, yes, you use UIAlertView in iOS 7 and UIAlertController in iOS 8.

Programming for iOS 7.1, and using a UIAlertView, will this work for iOS 8.0 as well?

You should do it either way. It doesn't matter as long as it works.

If your app supports multiple iOS versions and a certain API is only deprecated is some of the app supported iOS versions then there no problem using just the older API (again, assuming it still works with the newer iOS).

Think of this way - you have two options:

  1. Use just UIAlertView for now. It works as your app supports both iOS 7 and 8. Someday, maybe next year, you decided to drop support for iOS 7. At that time you can replace all uses of UIAlertView with UIAlertController. It's a onetime hit to do the change.
  2. Update your code like in the other answers. Write code that checks now if UIAlertController is available. Use if it is, else use UIAlertView. Then next year you drop iOS 7 support and then you update all of the existing code to remove the check for UIAlertController and remove the code to use UIAlertView. So you add lots of code now and make a bunch of changes later. That's twice the work of option 1 for no gain.

How to use UIAlertView with iOS 8?

AlertView in Swift in ios8

var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)


Related Topics



Leave a reply



Submit