I have two files .h and .m with some util methods to be called from viewcontrollers. I have one with an alertview, which when the ok button is clicked, the app would terminate.
I will use the next code to exit, after user click the button:
[[UIApplication sharedApplication] terminateWithSuccess];
I have a warning saying me that UIApplication shared doesn't have an interface.
My code:
#pragma mark - SHOW ALERTVIEW FOR IOS 7 or less AND IOS 8
+(void) showQuitAlert:(NSString*)alertTitle withMessage:(NSString *)alertMessage{
NSString *alertOkButtonText = @"Accept";
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO( NSFoundationVersionNumber_iOS_8_0 ) ) {
NSLog(@"iOS 8 dialog process");
id rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
rootViewController = [((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
}
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle
message:alertMessage
preferredStyle:UIAlertControllerStyleAlert];
//We add buttons to the alert controller by creating UIAlertActions:
UIAlertAction *actionOk = [UIAlertAction actionWithTitle:alertOkButtonText
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alertController dismissViewControllerAnimated:YES completion:nil];
[[UIApplication sharedApplication] terminateWithSuccess];
}];
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];
[alertController addAction:actionOk];
[rootViewController presentViewController:alertController animated:YES completion:nil];
}
if (SYSTEM_VERSION_LESS_THAN(NSFoundationVersionNumber_iOS_7_0) ) {
NSLog(@"iOS 7 or less dialog process");
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:alertTitle
message:alertMessage
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:alertOkButtonText, nil];
[alertView show];
}
}
Thanks