-4

I want to quit the application if it is not able to download the database from the server. I try to download the database using NSURLSession. if it doesn't then show the alert view.

when user clicks on ok button in the alert, then i want to quit the application. As there is no database, it will crash to proceed. To avoid the crash i want to quit from the app programatically.

I want to achieve something like android's finish().

I have one util.h and util.m which does all download show alert box job. It extends NSObject. It is just like an utility not a controller.

Util.h

#import <Foundation/Foundation.h>


@interface Util : NSObject {


NSURLSession *session;
//NSDictionary *plistDictionary;
NSURLSessionTask *task1;
NSURLSessionTask *task2;
NSURLSessionTask *task3;
UIAlertView *alertViewSpin;
NSMutableData *receivedData;

}

I have seen in one of the SO post..

 [self dismissViewControllerAnimated:YES completion:nil];
 [self.navigationController popViewControllerAnimated: YES];

But did not help. How can I do this?

Anjali
  • 1,623
  • 5
  • 30
  • 50
  • If you do this Apple will reject your app from the Apple Review process as it is not allowed to just quit the app even if you tell them. So what you want is **NOT** possible. – Popeye Jul 21 '15 at 11:19
  • 2
    why you will kill the app? instead of kill, retry the download... don't go ahead untill download success... – Fahim Parkar Jul 21 '15 at 11:24

4 Answers4

5

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.

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.

Note that using exit(0) or [[NSThread mainThread] exit] may cause your app to be refused in the App-store submission

More information

Community
  • 1
  • 1
Alaeddine
  • 6,104
  • 3
  • 28
  • 45
2

IMO, Killing the app just because the download is failed is worst idea...

What I would suggest is if download fail, show alert to user that to use the app, content has to be downloaded from the server. And show, Retry and Close app button. When user go for Retry, try to download again. So this way you can control what you want without killing the app. You will show content, only if download is complete

Let me know if you need any further details.

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
1

As others have said, it is not possible and not suggested to quit the app.

Alternatively, how about present a modal view controller, which tells the user what happened?

I have seen Uber did it once, telling me if I don't upgrade I won't be able to use this app anymore.

Jakehao
  • 1,019
  • 8
  • 15
0

Already answered in this post:- Proper way to exit iPhone application?

Please read this guideline before using it:-

Never quit an iOS app programmatically. People tend to interpret this as a crash. If something prevents your app from functioning as intended, you need to tell users about the situation and explain what they can do about it. APPLE Guideline

For you case, so use it on your risk:-

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

   if (buttonIndex != 0)  // 0 == the cancel button
   {

      exit(0); // Close the app
  }
}
Community
  • 1
  • 1
Vizllx
  • 9,135
  • 1
  • 41
  • 79