Quit iOS Application Programmatically with UIAlertView

允我心安 提交于 2019-12-08 13:52:44

问题


I'm aborting my iOS Application by below methods

-(void)cancelSelected
{
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil message:@"Are you sure you want to exit?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];

    [alert show];

    alert = nil;
}

Method 1 :

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex)
        abort();
}

Method 2 :

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex)
        [NSException raise:@"Disagree terms and conditions." format:@"Quit, Cancel"];
}

Shall I do this to quit my iOS Application programmatically?

Will this abort() method leads to reject my app?

Thanks!


回答1:


See QA1561:

Q: How do I programmatically quit my iOS application?

A: There is no API provided for gracefully terminating an iOS application.

In iOS, the user presses the Home button to close applications. Should your application have conditions in which it cannot provide its intended function, the recommended approach is to display an alert for the user that indicates the nature of the problem and possible actions the user could take — turning on WiFi, enabling Location Services, etc. Allow the user to terminate the application at their own discretion.




回答2:


Yes, generally you will get rejected for that.

Just present an alert to the user with a singe option, so they must approve to dismiss the alert. Then, if they dismiss (approve) they can use the app and if they don't they can't and must quit the app manually.




回答3:


Yes the codes above will result in a reject. Use this code instead in your OK button of alert:

UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)



回答4:


UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Game Over" message:@"Your time is up" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *close = [UIAlertAction actionWithTitle:@"close" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                                                                 **exit(0);**
                                                             }];
UIAlertAction *playagain = [UIAlertAction actionWithTitle:@"Play again" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                                                                  [self viewDidLoad];
                                                             }];

[alert addAction:close];
[alert addAction:playagain];

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

Use exit(0) for close current application




回答5:


You can use below code to Quit iOS Application Programmatically with UIAlertView :-

Step 1:

Delegate "UIAlertViewDelegate" to your viewcontroller.h

for example:
 @interface User_mail_List : UIViewController< UIAlertViewDelegate >

Step 2:

//create your UIAlertView
UIAlertView  *exit_alertView= [[UIAlertView alloc] initWithTitle:@"Bizbilla !" message:@"\nAre you sure you want to Exit ?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes",nil];
[exit_alertView show];

Step 3:

-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
if(alertView==exit_alertView){//exit Alert Fns Start,,,,,
    if(buttonIndex==1){
        exit(0);
    }
}//exit Alert Fns End,,,,,

}

thanks,




回答6:


On your alertview button click

You can use: exit(0)?

Or,

[[NSThread mainThread] exit], using this you can quit ios app.




回答7:


How about calling fatalError() function? I've just used it, everything works as expected. (Hope this will not cause a rejection though.)



来源:https://stackoverflow.com/questions/23391706/quit-ios-application-programmatically-with-uialertview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!