Uialertview First Deprecated iOS 9

UIAlertView first deprecated IOS 9

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 is deprecated: first deprecated in iOS 9.0

You're getting these warnings/errors because those methods have been removed from the code base. I'm guessing you're trying to follow along with an old tutorial.

You should also post more of your code. What you have shown us is not where your warnings/errors are.

For the dismissModalViewControllerAnimated use this instead.

[self dismissViewControllerAnimated:YES completion:nil];

For presentModalViewController:animated use this.

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

Finally, for your UIAlertView you should now be using a UIAlertController:

UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"title"
message:@"some message"
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(@"Cancel action");
}];

UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"OK", @"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"OK action");
}];

[alertController addAction:cancelAction];
[alertController addAction:okAction];

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

UIAlertView' was deprecated in iOS 9.0. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead

See this Code Destructive and OK buttons in UIAlertController:

let alertController = UIAlertController(title: "Destructive", message: "Simple alertView demo with Destructive and Ok.", preferredStyle: UIAlertControllerStyle.alert) //Replace UIAlertControllerStyle.Alert by UIAlertControllerStyle.alert
let DestructiveAction = UIAlertAction(title: "Destructive", style: UIAlertActionStyle.Destructive) {
(result : UIAlertAction) -> Void in
print("Destructive")
}

// Replace UIAlertActionStyle.Default by UIAlertActionStyle.default
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
(result : UIAlertAction) -> Void in
print("OK")
}

alertController.addAction(DestructiveAction)
alertController.addAction(okAction)
self.presentViewController(alertController, animated: true, completion: nil)

Swift 3:

let alertController = UIAlertController(title: "Destructive", message: "Simple alertView demo with Destructive and Ok.", preferredStyle: UIAlertControllerStyle.alert) //Replace UIAlertControllerStyle.Alert by UIAlertControllerStyle.alert

let DestructiveAction = UIAlertAction(title: "Destructive", style: UIAlertActionStyle.destructive) {
(result : UIAlertAction) -> Void in
print("Destructive")
}

// Replace UIAlertActionStyle.Default by UIAlertActionStyle.default

let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
(result : UIAlertAction) -> Void in
print("OK")
}

alertController.addAction(DestructiveAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)

See Alert With Destructive and OK Button:

Sample Image

I keep getting this error 'UIAlertView' was deprecated in iOS 9.0

As the error states, UIAlertView was deprecated as of iOS 9.0. UIAlertController was added in iOS 8.0. Your code is correctly choosing to use UIAlertController under iOS 8.0 or later.

The problem is that your code tries to use UIAlertView for anything earlier than iOS 8.0. But your app's Deployment Target is not set for iOS 7 or earlier. Therefore the compiler is telling you that UIAlertView is deprecated.

Since you are not actually going to be supporting anything earlier than iOS 8.0, there is no need for the if #available(iOS 8.0, *) or for UIAlertView. Just write your code using UIAlertController and don't worry about UIAlertView.

All of the code you posted can be reduced to just the UIAlertController part:

let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertControllerStyle.alert)

alert.addAction(UIAlertAction(title: alertOKTitle, style:.destructive, handler: { alertAction in
self.okButtonPressed()
alert.dismiss(animated: true, completion: nil)
}))

alert.addAction(UIAlertAction(title: alertCancelTitle, style:.cancel, handler:{ alertAction in
self.cancelButtonPressed()
alert.dismiss(animated: true, completion: nil)
}))

alert.addAction(UIAlertAction(title: alertRemindLaterTitle, style:.default, handler: { alertAction in
self.remindLaterButtonPressed()
alert.dismiss(animated: true, completion: nil)
}))

let appDelegate = UIApplication.shared.delegate as! AppDelegate
let controller = appDelegate.window?.rootViewController

controller?.present(alert, animated: true, completion: nil)

UIAlertView is Deprecated how can i fix this code as UIAlertController? I can't figure out how to use switch statement in UIAlertController

AlertView is depricated in iOS 8.So we need to use UIAlertController.

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

UIAlertAction *actionAccessAddressbook = [UIAlertAction
actionWithTitle:@"Access"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self displayFindFriendView:[NSNumber numberWithInteger: CS_CONTACTS ]];
}];

UIAlertAction *actionFindFriendEmail = [UIAlertAction
actionWithTitle:@"Find Friend Email"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//...Do your stuff here
}];

UIAlertAction *actionLogout = [UIAlertAction
actionWithTitle:@"Logout"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self userLogout];
}];
UIAlertAction *actionClearSearchHistory = [UIAlertAction
actionWithTitle:@"ClearSearchHistory"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[[CSCoreDataHandler sharedInstance] deleteManagedObjectsInModel:@"CSRecentSearch"];
}];


[alert addAction:actionAccessAddressbook];
[alert addAction:actionFindFriendEmail];
[alert addAction:actionLogout];
[alert addAction:actionClearSearchHistory];

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

How do I migrate from UIAlertView (deprecated in iOS8)

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)



Related Topics



Leave a reply



Submit