6

At some point in my application I have done this exit(0) which crashes my app. But I haven't figured out what method gets called when this executes.

I've put messages in:

(void)applicationWillTerminate:(UIApplication *)application
(void)applicationDidEnterBackground:(UIApplication *)application

But none of this seem to get called! Any idea about what method is called when exit(0) is done?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
adrian
  • 4,574
  • 17
  • 68
  • 119

5 Answers5

24

From Apple's Human User Guidelines...

Don’t Quit Programmatically

Never quit an iOS application programmatically because people tend to interpret this as a crash. However, if external circumstances prevent your application from functioning as intended, you need to tell your users about the situation and explain what they can do about it. Depending on how severe the application malfunction is, you have two choices.

Display an attractive screen that describes the problem and suggests a correction. A screen provides feedback that reassures users that there’s nothing wrong with your application. It puts users in control, letting them decide whether they want to take corrective action and continue using your application or press the Home button and open a different application

If only some of your application's features are not working, display either a screen or an alert when people activate the feature. Display the alert only when people try to access the feature that isn’t functioning.

If you've decided that you are going to quit programmatically anyway...

In C, exit(0) will halt execution of the application. This means that no delegate methods or exception handlers will be called. So, if the goal is to make sure that some code gets called when the closes, even on a forced close, there may be another option. In your AppDelegate implement a custom method called something like -(void)applicaitonIsgoingAway. Call this method from within anywhere you want your exiting code to be called:

  1. applicationWillTerminate
  2. applicationDidEnterBackground
  3. onUncaughtException

The first two are ones that you already mentioned in your question. The third can be a catch-all of sorts. It's a global exception handler. This next bit comes from a question on that very topic.

This exception handler will get called for any unhanded exceptions (which would otherwise crash your app). From within this handler, you can call applicaitonIsgoingAway, just like in the other 2 cases. From the other question that I mentioned above, you can find an answer similar to this.

void onUncaughtException(NSException* exception)
{
    [[AppDelegate sharedInstance] applicationIsgoingAway];
}

But in order for this to work, you need to set this method up as the exception handler like so...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  NSSetUncaughtExceptionHandler(&onUncaughtException);
 //There may already be more code in this method.
}

Now, you can quit the app programmatically by calling NSAssert(FALSE, @"Quitting the app programmatically."); As long as there is no other exception handler in place to catch this, your app will begin to crash, and your exception handler code will be called. in-turn calling applicationIsGoingAway.

Community
  • 1
  • 1
Jeff Wolski
  • 6,332
  • 6
  • 37
  • 69
  • 1
    The Apple reference cited is no longer contained in the latest Apple HIG: https://developer.apple.com/design/human-interface-guidelines/ios/overview/interface-essentials/ – Native_Mobile_Arch_Dev Apr 17 '19 at 14:38
  • @Native_Mobile_Arch_Dev: You mean to say they are now ok if apps are using exit(0) ? – Tejas K Nov 20 '19 at 06:48
  • 1
    Hi @TejasK - Actually, I'm not implying anything and no inferences can be made in this case. I'm simply asserting that the Apple reference cited is no longer contained in the latest Apple HIG. – Native_Mobile_Arch_Dev Nov 20 '19 at 15:51
10

When you call exit(0) you immediately terminate your application. 0 is a status code which means successful termination.

No other method is called, you application just dies. As a result end user may think app is just crashed.

Apple discourages you to call exit anywhere.

dimme
  • 4,393
  • 4
  • 31
  • 51
  • tehn how I get out of my app in arecommended way, but programatically? – adrian Dec 13 '11 at 15:16
  • 4
    @george: You don't. The user has to actively chose to close the application. – dimme Dec 13 '11 at 15:32
  • When I call exit(0) (testing only, I know it is not allowed/recommended), the app still shows in the background application list. – Siriss Dec 11 '13 at 17:03
2

exit(0) is a C function that terminates your app's process therefore none of the application delegates methods will be called, the app will be killed immediately. Apple recommends strongly against your app quitting because it appears broken to the user.

Matthew Bischoff
  • 1,043
  • 11
  • 27
0

There is no Apple-supported method to terminate your application programmatically. Calling exit is certainly out of the question. This causes all sorts of bugs (for example the multitasking switcher will break badly) as well as simply being wrong.

If you are trying to disable multitasking, you can do this with the UIApplicationExitsOnSuspend key in your Info.plist file (the title for the key is "Application does not run in background").

Other than that, it's up to your users to press the home button to close your application.

Mike Weller
  • 45,401
  • 15
  • 131
  • 151
  • 1
    There are valid use-cases for calling `exit(0)`.  A portion of an app's runtime may crash (for example, in an app using Mono), and the only safe course of action remaining for the Obj-C code to take is to log an error and die immediately. – Slipp D. Thompson Feb 01 '15 at 00:23
-5

these methods will be called but you cannot use exit(0) you will need to press the back button to close your app then these methods will be called

owen gerig
  • 6,165
  • 6
  • 52
  • 91
  • 4
    Back button to close an app? This isn't Android. – Maciej Swic Jun 10 '13 at 12:47
  • my statement is correct and just because I call it a back button doesnt change that. you cannot use exit 0 and the only way to close an app is with the physical button (whatever you want to call it) – owen gerig Jun 10 '13 at 14:19
  • 3
    Well, your statement is not correct since the name of that button is the *home* button and not the back button. – Maciej Swic Jun 11 '13 at 08:49