I am programming an iPhone app, and I need to force it to exit due to certain user actions. After cleaning up memory the app allocated, what’s the appropriate method to call to terminate the application?

22 Solutions Collect From Internet About “Proper way to exit iPhone application?”

Have you tried exit(0)?

Alternatively, [[NSThread mainThread] exit], although I have not tried that it seems like the more appropriate solution.

On the iPhone there is no concept of quitting an app. The only action that should cause an app to quit is touching the Home button on the phone, and that’s not something developers have access to.

According to Apple, your app should not terminate on its own. Since the user did not hit the Home button, any return to the Home screen gives the user the impression that your app crashed. This is confusing, non-standard behavior and should be avoided.

exit(0) appears to a user as crashes, so show a confirmation message to user. After confirmation suspend(home button press programmatically) and wait 2 seconds while app is going background with animation then exit behind user’s view

Q: How do I programmatically quit my iOS application?

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.

Additionally, data may not be saved, because -applicationWillTerminate: and similar UIApplicationDelegate methods will not be invoked if you call exit.

If during development or testing it is necessary to terminate your application, the abort function, or assert macro is recommended

using the private interface : [UIApplication sharedApplication] will cause the app looking like it crashed, BUT it will call - (void)applicationWillTerminate:(UIApplication *)application before doing so;

using exit(0); will also terminate the application, but it will look “normal” (the springboard’s icons appears like expected, with the zoom out effect), BUT it won’t call the - (void)applicationWillTerminate:(UIApplication *)application delegate method.

My advice:

Manually call the - (void)applicationWillTerminate:(UIApplication *)application on the delegate.

Call exit(0);.

Your ApplicationDelegate gets notified of intentional quitting by the user:

- (void)applicationWillResignActive:(UIApplication *)application {

When I get this notification I just call

exit(0);

Which does all the work. And the best thing is, it is the useres intent to quit, which is why this should not be a problem calling it there.

On my Audio-App it was necessary to quit the app after people were syncing their device while the music was still playing. As soon as the syncing is complete I get a notification. But quitting the app right after that would actually look like a crash.

So instead I set a flag to REALLY quit the app on the next backgrounding action. Which is okay for refreshing the app after a sync.

My App has been rejected recently bc I’ve used an undocumented method. Literally:

“Unfortunately it cannot be added to the App Store because it is using a private API. Use of non-public APIs, which as outlined in the iPhone Developer Program License Agreement section 3.3.1 is prohibited:

“3.3.1 Applications may only use Documented APIs in the manner prescribed by Apple and must not use or call any private APIs.”

The non-public API that is included in your application is terminateWithSuccess”

Apple say:

“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.”

I think that this is a bad assumption. If the user tap a quit button and a message appears that say something like: “The application will now quit.”, it doesn’t appear to be crashed. Apple should provide a valid way to quit an application (not exit(0)).

If you ever have a problem with Apple platform you can’t easily find a solution for, consult HIG. It’s possible Apple simply doesn’t want you to do it and they usually (I’m not Apple so I can’t guarantee always) do say so in their documentation.

We can not quit app using exit(0), abort() functions, as Apple strongly discourage the use of these functions. Though you can use this functions for development or testing purpose.

If during development or testing it is necessary to terminate your
application, the abort function, or assert macro is recommended

Please find this Apple Q&A thread to get more information.

As use of this function create impression like application is crashing. So i got some suggestion like we can display Alert with termination message to aware user about closing the app, due to unavailability of certain functionality.

But iOS Human Interface Guideline for Starting And Stopping App, suggesting that Never use Quit or Close button to terminate Application. Rather then that they are suggesting to display proper message to explain situation.

An iOS app never displays a Close or Quit option. People stop using an
app when they switch to another app, return to the Home screen, or put
their devices in sleep mode.

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.

In addition to the above, good, answer I just wanted to add, think about cleaning up your memory.

After your application exits, the iPhone OS will automatically clean up anything your application left behind, so freeing all memory manually can just increase the amount of time it takes your application to exit.

Hm, you may ‘have to’ quit the application if, say, your application requires an internet connection. You could display an alert and then do something like this:

But still not meant for production in my case. It is for testing crash reportings, or to fast restart after a Core Data reset. Just made it safe not to be rejected if function left in the production code.

I used the [[NSMutableArray new] addObject:nil] approach mentioned above to force-quit (crash) the app without making a tell-tale exit(0) function call.

If certificate authentication fails, all of my initialization calls error out and leave my app in an indeterminate state. Letting the user go home and then back into the app doesn’t help, as unless the app has been purged by the OS it’s still uninitialized and untrustworthy.

So, in this one case, we deemed it best to pop an alert informing the user that the app is operating in an insecure environment and then, when they hit “Close”, force quit the app using the aforementioned method.

The user should decide when an app exits.
I don’t think it is a good user interaction when an app quits. Therefore there is no nice API for it, only the home button has one.

If there is an error: Implement it better or Notify the user.
If there have to be a restart: Implement it better of Notify the user.

It sounds dumb, but it’s bad practice to exit the app without letting the user decide and not notifying him. And since there is a home button for the user interaction, Apple states, there should not be 2 things for the same function (exiting an app).

Exit an app other way

I did this helper, though, that use no private stuff:

Exit(0);

It may be appropriate to exit an app if it is a long lived app that also executes in the background, for example to get location updates (using the location updates background capability for that).

For example, let’s say the user logs out of your location based app, and pushes the app to the background using the home button. In this case your app may keep running, but it could make sense to completely exit it. It would be good for the user (releases memory and other resources that don’t need to be used), and good for app stability (i.e. making sure the app is periodically restarted when possible is a safety net against memory leaks and other low memory issues).

This could (though probably shouldn’t, see below 🙂 be achieved with something like:

Since the app would then exit out of the background it will not look wrong to the user, and will not resemble a crash, providing the user interface is restored the next time they run the app. In other words, to the user it would not look any different to a system initiated termination of the app when the app is in the background.

Still, it would be preferable to use a more standard approach to let the system know that the app can be terminated. For example in this case, by making sure the GPS is not in use by stopping requesting location updates, including turning off show current location on a map view if present. That way the system will take care of terminating the app a few minutes (i.e. [[UIApplication sharedApplication] backgroundTimeRemaining]) after the app enters the background. This would get all the same benefits without having to use code to terminate the app.