17

UAlertView is deprecated in iOS 9 and later. What would be an alternative?

UIAlertView *new = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Your InApp Purchases were successfully restored" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[new show];
Ketan P
  • 4,259
  • 3
  • 30
  • 36
user3138007
  • 637
  • 1
  • 6
  • 19
  • 1
    Any reason you didn't simply looks at the docs for `UIAlertView` (which tells you exactly what to do) or do a search before posting this question? Please make some attempt to find an answer before posting. – rmaddy Oct 05 '15 at 21:28
  • if you don't find answer in their doc, better shift your project to iOS 8. :P – Sazzad Hissain Khan Dec 27 '15 at 18:33

7 Answers7

50

You can use this code to replace an alert view:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];           
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];

If you need multiple actions you can use:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"Button 1" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    // action 1
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"Button 2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    // action 2
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    [self dismissViewControllerAnimated:YES completion:nil];
}]];           
[self presentViewController:alertController animated:YES completion:nil];
Ricardo Sanchez-Saez
  • 9,466
  • 8
  • 53
  • 92
Nithinbemitk
  • 2,710
  • 4
  • 24
  • 27
  • 2
    This takes so much code to implement... What are they thinking? The ability to handle responses without a delegate is a nice addition though. – JRam13 Mar 25 '16 at 15:00
  • @Kundapra Hudga, why did you choose to use dispatch_async for [self presentViewController:alertController animated:YES completion:nil]; ? – Adela Toderici Jul 06 '16 at 20:20
  • You are use the UIAlertController, See for Swift AlertController tutorial : https://iosdevcenters.blogspot.com/2016/03/uialertcontroller-in-swift.html – Kirit Modi Sep 14 '16 at 05:23
  • Why presentViewController is called from dispatch queue? – Paweł Brewczynski Jan 03 '17 at 17:00
9

You get often detailed information including the replacement suggestion by -clicking on the symbol which displays the class/method declaration.

In case of UIAlertView you will see

"UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead"

vadian
  • 274,689
  • 30
  • 353
  • 361
3
  UIAlertController * alert=   [UIAlertController
                             alertControllerWithTitle:@"Info"
                             message:@"You are using UIAlertController"
                               preferredStyle:UIAlertControllerStyleAlert];

   UIAlertAction* ok = [UIAlertAction
                    actionWithTitle:@"OK"
                    style:UIAlertActionStyleDefault
                    handler:^(UIAlertAction * action)
                    {
                        [alert dismissViewControllerAnimated:YES completion:nil];

                    }];
  UIAlertAction* cancel = [UIAlertAction
                         actionWithTitle:@"Cancel"
                       style:UIAlertActionStyleDefault
                       handler:^(UIAlertAction * action)
                       {
                           [alert dismissViewControllerAnimated:YES completion:nil];
                        }];
 [alert addAction:ok];
 [alert addAction:cancel];

 [self presentViewController:alert animated:YES completion:nil];
Jaywant Khedkar
  • 5,941
  • 2
  • 44
  • 55
Mr.Javed Multani
  • 12,549
  • 4
  • 53
  • 52
2

I made a category for that:

+ (void)alertViewWithTitle:(NSString *)aTitle message:(NSString *)aMessage viewController:(UIViewController *) aVC
{
    UIAlertController * alert = [UIAlertController
                                 alertControllerWithTitle:aTitle ? aTitle : @""
                                 message:aMessage
                                 preferredStyle:UIAlertControllerStyleAlert];

    UIViewController *topVC = aVC ? aVC : [UIApplication sharedApplication].keyWindow.rootViewController;
    [topVC presentViewController:alert animated:YES completion:nil];
}

The parameters aTitle and aVC are optional but aVC should be used if known.

PS: avoid the "new" as a variable name this is a reserved word, I actually don't know if it will compile though.

Rémy Blanc
  • 313
  • 2
  • 9
1

UIAlertController has been around since iOS 8.

quark
  • 1,725
  • 1
  • 14
  • 23
1
UIAlertController * alert=   [UIAlertController
                                    alertControllerWithTitle:@"My Title"
                                    message:@"Enter User Credentials"
                                    preferredStyle:UIAlertControllerStyleAlert];

    [self presentViewController:alert animated:YES completion:nil];
user3138007
  • 637
  • 1
  • 6
  • 19
1

I have use "UIAlertController" on iOS 8 and later. Let see:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Success" message:@"Your InApp Purchases were successfully restored" preferredStyle:UIAlertControllerStyleAlert];

And add buttons:

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
           //do something when click button
}];

Remember:

[alertController addAction:okAction];

Then show it:

[self presentViewController:alertController animated:YES completion:nill];

If you want to show a actionsheep, you change

"preferredStyle:UIAlertControllerStyleActionSheet"
Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
AmyNguyen
  • 437
  • 6
  • 10