2

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!

Natarajan
  • 3,241
  • 3
  • 17
  • 34
  • Yes, it probably will – Scott Berrevoets Apr 30 '14 at 15:08
  • https://developer.apple.com/library/ios/qa/qa1561/_index.html – rdurand Apr 30 '14 at 15:10
  • It will be 50-50. They may or may not notice it. Other way is - make it quit, do something unexpected - call a method on `nil`...that way, the app will crash and quit. – Michal Apr 30 '14 at 15:11
  • possible duplicate of [Is it possible to quit iOS app after we do some checks](http://stackoverflow.com/questions/12242319/is-it-possible-to-quit-ios-app-after-we-do-some-checks) – rmaddy Apr 30 '14 at 15:11
  • @rmaddy This question should not be a duplicate. Because of I have used UIAlertView to show the actions to users. – Natarajan Apr 30 '14 at 15:40
  • Using an alert view is irrelevant to the question. The question is about quitting an app. Apple makes it clear that you are not to quit the app. It's the user's choice. – rmaddy Apr 30 '14 at 15:42
  • Shall I use NSException to quit the app? – Natarajan Apr 30 '14 at 15:43
  • possible duplicate of [How can I close an iPad app in Objective-C?](http://stackoverflow.com/questions/4649320/how-can-i-close-an-ipad-app-in-objective-c) – Yucel Bayram Dec 05 '14 at 08:18

7 Answers7

11

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.

Droppy
  • 9,691
  • 1
  • 20
  • 27
6

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

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.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I have assumed that really you want to elicit some kind of approval from the user. If you are really offering a `close` button, then you should just be removing that and doing nothing instead. – Wain Apr 30 '14 at 15:10
3
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

Chandresh Kachariya
  • 667
  • 2
  • 13
  • 31
Dharmesh Mansata
  • 4,422
  • 1
  • 27
  • 33
  • Warning: Do not call the exit function. Applications calling exit will appear to the user to have crashed, rather than performing a graceful termination and animating back to the Home screen. https://developer.apple.com/library/content/qa/qa1561/_index.html – seggy Nov 01 '17 at 10:41
2

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,

Kupendiran iOS
  • 217
  • 2
  • 5
1

On your alertview button click

You can use: exit(0)?

Or,

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

Vin
  • 10,517
  • 10
  • 58
  • 71
ravi sendhav
  • 177
  • 10
0

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

Murat Yasar
  • 994
  • 9
  • 24