UIAlertView first deprecated IOS 9

uialertview deprecated xamarin
uialertcontroller objective-c
uialertview swift 4
uialertcontrollerstylealert
custom uialertview

I have tried several ways to use UIAlertController,instead of UIAlertView. I tried several ways but I cannot make the alert action work. Here is my code that works fine in IOS 8 and IOS 9 but is showing up with deprecated flags. I tried the elegant suggestion below but I can't make it function in this context. I need to submit my app and this is the last thing to address. Thank You for any further suggestions. I am a newbie.

#pragma mark - BUTTONS ================================
- (IBAction)showModesAction:(id)sender {
NSLog(@"iapMade: %d", iapMade3);

// IAP MADE ! ===========================================
if (!iapMade3) {

    //start game here
    gamePlaysCount++;
    [[NSUserDefaults standardUserDefaults]setInteger:gamePlaysCount forKey:@"gamePlaysCount"];
    NSLog(@"playsCount: %ld", (long)gamePlaysCount);

    if (gamePlaysCount >= 4) {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Basic"
                                                     message: THREE_PLAYS_LIMIT_MESSAGE
                                                    delegate:self
                                           cancelButtonTitle:@"Yes, please"
                                           otherButtonTitles:@"No, thanks", nil];
       [alert show];

        NSString *path = [[NSBundle mainBundle] pathForResource:@"cow" ofType:@"wav"];
        _pop =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
        [_pop play];
        [self dismissViewControllerAnimated:true completion:nil];

    } else {
        if (gamePlaysCount == 1)  {
            // Create & store the next 5 mins when player gets 3 more lives
            nextDateToPlay = [[NSDate date] dateByAddingTimeInterval:60*60*0.1];
            NSLog(@"CURRENT DATE: %@", [NSDate date]);
            NSLog(@"NEXT DAY: %@", nextDateToPlay);
            [[NSUserDefaults standardUserDefaults]setObject: nextDateToPlay    forKey:@"nextDateToPlay"];
            NSLog(@"nextDateToPlay: %@", nextDateToPlay);

            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Basic"
                                                           message:  THREE_PLAYS_LIMIT_MESSAGE2
                                                          delegate:self
                                                 cancelButtonTitle:@"Got it!"
                                                 otherButtonTitles:@"Start", nil];
            [alert show];
        } else {

            if (gamePlaysCount == 3)  {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Basic"
                                                               message: THREE_PLAYS_LIMIT_MESSAGE3
                                                              delegate:self
                                                     cancelButtonTitle:@"Yep, I Know!"
                                                     otherButtonTitles:@"Start", nil];
                [alert show];
            }
        }
    }
}

}

// IAP NOT MADE =============================

#pragma mark - ALERTVIEW DELEGATE ============================

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:@"Yes, please"]) {

    UIStoryboard *storyboard = self.storyboard;
    MenuViewController *svc = [storyboard instantiateViewControllerWithIdentifier:@"Store"];
    svc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:svc animated:YES completion:nil];

        }

}

From iOS8 Apple provide new UIAlertController class which you can use instead of UIAlertView which is now deprecated, it is also stated in deprecation message:

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

So you should use something like this

UIAlertController * alert = [UIAlertController
                alertControllerWithTitle:@"Title"
                                 message:@"Message"
                          preferredStyle:UIAlertControllerStyleAlert];



UIAlertAction* yesButton = [UIAlertAction
                    actionWithTitle:@"Yes, please"
                              style:UIAlertActionStyleDefault
                            handler:^(UIAlertAction * action) {
                                //Handle your yes please button action here
                            }];

UIAlertAction* noButton = [UIAlertAction
                        actionWithTitle:@"No, thanks"
                                  style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action) {
                                   //Handle no, thanks button                
                                }];

[alert addAction:yesButton];
[alert addAction:noButton];

[self presentViewController:alert animated:YES completion:nil];

'UIAlertView' was deprecated in iOS 9.0. Use UIAlertController with a , See this Code Destructive and OK buttons in UIAlertController : let alertController = UIAlertController(title: "Destructive", message: "Simple  warning: 'UIAlertView' is deprecated: first deprecated in iOS 9.0 - UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead. Detecting the iOS version, and if is the 9+ use this new controller UIAlertController, if is old version use the UIAlertView.

//Calling     
[self showMessage:@"There is no internet connection for this device"
                    withTitle:@"Error"];

//Method

-(void)showMessage:(NSString*)message withTitle:(NSString *)title
{

 UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:title
                                  message:message
                                  preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

        //do something when click button
    }];
    [alert addAction:okAction];
    UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
    [vc presentViewController:alert animated:YES completion:nil];
}

If you want to use this alert in NSObject class you should use like:

-(void)showMessage:(NSString*)message withTitle:(NSString *)title{
dispatch_async(dispatch_get_main_queue(), ^{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    }]];

    [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alertController animated:YES completion:^{
    }];
});
}

UIAlertView, In apps that run in versions of iOS prior to iOS 8, use the UIAlertView class to display an alert message to the user. An alert view iOS 2.0–9.0Deprecated; Mac Catalyst 13.0–13.0Deprecated. Framework The index of the first other button. Alternative to UIAlertView for iOS 9? Ask Question Asked 4 years, 6 months ago. Active 1 year, 4 months ago. UIAlertView first deprecated IOS 9. Related. 0.

Swift version of new implementation is :

 let alert = UIAlertController(title: "Oops!", message:"your message", preferredStyle: .Alert)
 alert.addAction(UIAlertAction(title: "Okay.", style: .Default) { _ in })
 self.presentViewController(alert, animated: true){}

UIAlertView deprecated in iOS 9 · Issue #95 · analogcode/Swift , I guess with the new XCode new depreciations any ideas how to fixed this one? 'UIAlertView' was deprecated in iOS 9.0: UIAlertView is  Important: UIPopover Controller is deprecated in iOS 9. In iOS 9 and later, a popover is implemented as a UIView Controller presentation. To create a popover, use an instance of UIPopover Presentation Controller and specify the UIModal Presentation Style.popover style.

Xcode 8 + Swift

Assuming self is a UIViewController:

func displayAlert() {
    let alert = UIAlertController(title: "Test",
                                  message: "I am a modal alert",
                                  preferredStyle: .alert)
    let defaultButton = UIAlertAction(title: "OK",
                                      style: .default) {(_) in
        // your defaultButton action goes here
    }

    alert.addAction(defaultButton)
    present(alert, animated: true) { 
        // completion goes here
    }
}

UIAlertView first deprecated IOS 9, I have tried several ways to use UIAlertController,instead of UIAlertView. I tried several ways but I cannot make the alert action work. Here is my  GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. UIAlertView is deprecated in

Make UIAlertController+AlertController Category as:

UIAlertController+AlertController.h

typedef void (^UIAlertCompletionBlock) (UIAlertController *alertViewController, NSInteger buttonIndex);

@interface UIAlertController (AlertController)

+ (instancetype)showAlertIn:(UIViewController *)controller
                  WithTitle:(NSString *)title
                    message:(NSString *)message
          cancelButtonTitle:(NSString *)cancelButtonTitle
          otherButtonTitles:(NSString *)otherButtonTitle
                   tapBlock:(UIAlertCompletionBlock)tapBlock;
@end

UIAlertController+AlertController.m

@implementation UIAlertController (NTAlertController)

+ (instancetype)showAlertIn:(UIViewController *)controller
                  WithTitle:(NSString *)title
                    message:(NSString *)message
          cancelButtonTitle:(NSString *)cancelButtonTitle
          otherButtonTitles:(NSString *)otherButtonTitle
                   tapBlock:(UIAlertCompletionBlock)tapBlock {

    UIAlertController *alertController = [self alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

    if(cancelButtonTitle != nil) {

        UIAlertAction *cancelButton = [UIAlertAction
                                       actionWithTitle:cancelButtonTitle
                                       style:UIAlertActionStyleCancel
                                       handler:^(UIAlertAction *action)
                                       {
                                           tapBlock(alertController, ALERTACTION_CANCEL); // CANCEL BUTTON CALL BACK ACTION
                                       }];
        [alertController addAction:cancelButton];

    }

    if(otherButtonTitle != nil) {

        UIAlertAction *otherButton = [UIAlertAction
                                   actionWithTitle:otherButtonTitle
                                   style:UIAlertActionStyleDefault
                                   handler:^(UIAlertAction *action)
                                   {
                                       tapBlock(alertController, ALERTACTION_OTHER); // OTHER BUTTON CALL BACK ACTION
                                   }];

        [alertController addAction:otherButton];
    }

    [controller presentViewController:alertController animated:YES completion:nil];

    return alertController;
}

@end

in your ViewController.m

[UIAlertController showAlertIn:self WithTitle:@"" message:@"" cancelButtonTitle:@"Cancel" otherButtonTitles:@"Other" tapBlock:^(UIAlertController *alertController, NSInteger index){

 if(index == ALERTACTION_CANCEL){

 // CANCEL BUTTON ACTION
 }else
if(index == ALERTACTION_OTHER){

 // OTHER BUTTON ACTION
 }

 [alertController dismissViewControllerAnimated:YES completion:nil];

 }];

NOTE: If you want to add more than two buttons then add another more UIAlertAction to the UIAlertController.

UIAlertView first deprecated IOS 9, UIAlertView first deprecated IOS 9. j'ai essayé plusieurs façons d'utiliser UIAlertController,au lieu de UIAlertView. J'ai essayé plusieurs façons mais je ne peux  This class wrap UIAlertView (deprecated from iOS 9) and use the new UIAlertController class if the iOS is major of 8. This is transparently to the programmer hence is sufficient import this classes. An example of use is into the project. The utility of this classes is when in a old project there are more UIAlertView to change.

iOS Fundamentals: UIAlertView and UIAlertController, Apple has deprecated UIAlertView in iOS 8 though. 9. func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { The first benefit of using the UIAlertController class is the absence of a delegate  UIAlertView first deprecated IOS 9 j'ai essayé plusieurs façons d'utiliser UIAlertController,au lieu de UIAlertView. J'ai essayé plusieurs façons mais je ne peux pas faire fonctionner l'action d'alerte.

uialertview first deprecated ios 9, UIAlertView' is deprecated: first deprecated in iOS 9.0 - UIAlertView is deprecated​. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert  That's what I'm doing so far with UIAlertView's show method. This method is deprecated in iOS 9. How can I present a modal UIAlertController without knowing the ViewController currently displayed?

UIAlertView first deprecated IOS 9, I have tried several ways to use UIAlertController,instead of UIAlertView. I tried several ways but I cannot make the alert action work. Here is my  Hi Developer´s, In newer ios9, we have this warning: UIAlertView+AFNetworking.m:80:52: 'UIAlertView' is deprecated: first deprecated in iOS 9.0 - UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertController

Comments
  • Question not clear. What is it that you want ?
  • UIAlertView is deprecated in IOS 9 and we have to use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead. However, the alert does not show when I use UIAlertController methods. I tried the solution below, but still the alert view does not show.. Thank you..
  • Okay. Can you show what you have done and that doesn't work ?
  • After presenting you are calling [self dismissViewControllerAnimated:true completion:nil]; which will dismiss alert controller
  • This is an important question in the sense that UIAlertController is pretty new and many developers will be worried about the deprecation. The unreasonable down votes have bad effect on a valid question. Those who down voted the question should have changed their vote after the edit. But then,"down vote trolls" who get a badge for their down vote do not do this anyway. The SO moderators should have a way to fix it.
  • Thank you for your fast reply.. I tried this, but the alert view does not show.. Nor does it when I add my yes please action.
  • Can you show me your code, because its working fine for me, make sure you are executing this code on main thread
  • can you add your code inside question, that would be better every one can see that
  • Hi, I added my complete code that works in IOS8 and IOS9, but is flagged deprecated. I tried several ways to implement your code but the alert does not show, and the action to open the store ViewController, will not function. Thank you for your help.
  • I think it's better to remove this line [alert dismissViewControllerAnimated:YES completion:nil]; from the actions handlers, because the alert hides automatically without this code. Moreover, one can mistakenly put code in the completion block which will not be executed. Cheers :)
  • if self is view how we achieve this ?.
  • A view has no business in doing, well, the business logic. Rethink your architecture so that a controller presents views (MVVM or MVC)