How do I migrate from UIAlertView (deprecated in iOS8)

38,855

Solution 1

You need to use UIAlertController instead. To class documentation is pretty straightforward, even containing an usage example in Listing 1 at the very beginning of the doc (sure it's in ObjC and not in Swift but it's quite similar).

So for your use case, here is how it translates to (with added comments):

let alert = UIAlertController(title: "Oops!", message:"This feature isn't available right now", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default) { _ in
  // Put here any code that you would like to execute when
  // the user taps that OK button (may be empty in your case if that's just
  // an informative alert)
}
alert.addAction(action)
self.presentViewController(alert, animated: true){}

So the compact code will look like:

let alert = UIAlertController(title: "Oops!", message:"This feature isn't available right now", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
self.present(alert, animated: true){}

Where self here is supposed to be your UIViewController.


Additional tip: if you need to call that code that displays the alert outside of the context of an UIViewController, (where self is not an UIViewController) you can always use the root VC of your app:

let rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController
rootVC?.presentViewController(alert, animated: true){}

(But in general it's preferable to use a known UIViewController when you have one — and you generally present alerts from UIViewControllers anyway — or try to get the most suitable one depending on your context instead of relying on this tip)

Solution 2

For those wondering on how to do this in Objective-C:

    //Step 1: Create a UIAlertController
    UIAlertController *myAlertController = [UIAlertController alertControllerWithTitle:@"MyTitle"
                                                              message: @"MyMessage"
                                                              preferredStyle:UIAlertControllerStyleAlert                   ];

    //Step 2: Create a UIAlertAction that can be added to the alert
    UIAlertAction* ok = [UIAlertAction
                         actionWithTitle:@"OK"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             //Do some thing here, eg dismiss the alertwindow
                             [myAlertController dismissViewControllerAnimated:YES completion:nil];

                         }];

    //Step 3: Add the UIAlertAction ok that we just created to our AlertController
    [myAlertController addAction: ok];

    //Step 4: Present the alert to the user
    [self presentViewController:myAlertController animated:YES completion:nil];

This will pop-up an alert that looks like this:

alert example

Solution 3

let alertView = UIAlertView(title: "Oops!", message: "This feature isn't available right now", delegate: self, cancelButtonTitle: "OK")

becomes

let alertController = UIAlertController(title: "Oops!", message: "This feature isn't available right now", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in }
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true) { }

Solution 4

I think this is the way to have backward compatibility for older iOS SDK and to use new API when using newer SDK. Also it is without warnings for deprecation in code using deprecated class.

    if ([UIAlertController class]) {
        // Use new API to create alert controller, add action button and display it
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"CityBoard" message:error.errorDescription preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* ok = [UIAlertAction actionWithTitle: @"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
            [alertController dismissViewControllerAnimated:YES completion:nil];
        }];
        [alertController addAction: ok];
        [self presentViewController:alertController animated:YES completion:nil];
    } else {
        // We are running on old SDK as the new class is not available
        // Hide the compiler errors about deprecation and use the class available on older SDK
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"CityBoard"
                                                        message:error.errorDescription
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
#pragma clang diagnostic pop

Solution 5

Swift 2.0:

Use AlertController.

Example for Action Sheet:

  let mediaActionSheet: UIAlertController = UIAlertController(title: "Media Action Sheet", message: "Choose an option!", preferredStyle: .ActionSheet)

  //Create and add the Cancel action
  let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in

   //Just dismiss the action sheet

  }
  mediaActionSheet.addAction(cancelAction)
  //Create and add first option action
  let takePictureAction: UIAlertAction = UIAlertAction(title: "Take Picture", style: .Default) { action -> Void in

   //Code for launching the camera goes here

  }

  mediaActionSheet.addAction(takePictureAction)

  //Create and add a second option action
  let choosePictureAction: UIAlertAction = UIAlertAction(title: "Choose From Gallery", style: .Default) { action -> Void in

   //Code for picking from gallery goes here

  }

  mediaActionSheet.addAction(choosePictureAction)

  //Present the AlertController
  self.presentViewController(mediaActionSheet, animated: true, completion: nil)

Example for Alerts:

1)

let simpleAlert = UIAlertController(title: "Simple Alert", message: "It is just awesome", preferredStyle: UIAlertControllerStyle.Alert);

  //show it
  showViewController(simpleAlert, sender: self);

2) Alert With TextField in it.

  let inputTextFieldAlert:UIAlertController = UIAlertController(title: " Input TextField Alert ", message: " Enter on the below TextField ", preferredStyle: UIAlertControllerStyle.Alert);

  //default input textField (no configuration...)
  inputTextFieldAlert.addTextFieldWithConfigurationHandler(nil);

  //no event handler (just close dialog box)
  inputTextFieldAlert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.Cancel, handler: nil));

  //event handler with closure
  inputTextFieldAlert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: {(action:UIAlertAction) in

   let fields = inputTextFieldAlert.textFields!;
   print("Output: "+fields[0].text!);
  }));

  presentViewController(inputTextFieldAlert, animated: true, completion: nil);

3)

var alert = UIAlertController(title: "TextField Alert", message: "Enter on the below TextField", preferredStyle: UIAlertControllerStyle.Alert);

  //configured input textField
  var field:UITextField?;

  alert.addTextFieldWithConfigurationHandler({(input:UITextField)in
   input.placeholder="Empty Dtaa ;-)";
   input.clearButtonMode=UITextFieldViewMode.WhileEditing;

   field=input;
  });

  //YES Handler
  func yesHandler(actionTarget: UIAlertAction){

   print(field!.text!);
  }
  //event handler with predefined function
  alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: yesHandler));

  presentViewController(alert, animated: true, completion: nil);
Share:
38,855
Dom Bryan
Author by

Dom Bryan

Current university student working towards a computer science degree and hugely passionate about the tech industry. Hundreds if not thousands of hours spent learning programming in my spare time has lead me to love mobile development.

Updated on December 05, 2020

Comments

  • Dom Bryan
    Dom Bryan over 3 years

    I currently have the following line of code in one of my apps. It is a simple UIAlertView. However, as of iOS 8, this is now deprecated:

    let alertView = UIAlertView(title: "Oops!", message: "This feature isn't available right now", delegate: self, cancelButtonTitle: "OK")
    

    How do I update this to work with iOS 8+? I believe I have to change something to UIAlertCotroller, though I'm not too sure what.