Is it because a cancel button method is not implemented?
circled in blue cancel button not working
Below is my code. Where user is informed through a alert view that an updated version is available. Should OK be clicked, it would direct them to the app in iTunes App Store. (as shown in the pic below) However when cancel is clicked, nothing happens.
-(void)showAlert {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"New Version Available on Appstore" message:@"Please update app to continue" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UIApplication *application = [UIApplication sharedApplication];
NSURL *iTunesLink = [NSURL URLWithString:@"itms://itunes.apple.com/us/app/apple-store/id375380948?mt=8"];
[application openURL:iTunesLink options:@{} completionHandler:^(BOOL success) {
if (success) {
NSLog(@"Opened url");
}
}];
//button click event
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:ok];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
}
I manage to resolve this by adding "itms-apps" (straight away redirected to appstore with no cancel button). But I would still like to understand why the cancel button do not work
Since the cancel button in the web view is not created by me, is it possible to add a method to that?
My apologies for not being clear in my question. I would like to know why the circled cancel button in the screenshot do not work.
When you create an UIAlertController you can add to it different buttons as UIAlertActions that will do something when each one gets clicked. You will have to specify what you want to happen by implementing the handler in the initialization of the UIAlertAction (like you did above for the OK action).
If you pass nil as the handler the alert will simply be dismissed, but nothing else will happen because you did not tell it to do anything else.
The cancel button is related to the click on the alert button, not anything that you do after you click a different button, as maybe you'd like in you case.
More info here.