Programmatic way to terminate iPhone App?

Is there a way to programmatically terminate and iPhone app? There were several posts from last year about a terminate method on UIApplication but that selector was private and appears to no longer be valid (responds to selector is false). I haven't tried calling exit but I assume that won't call the normal shutdown mechanism (applicationwillterminate), etc.

Posted on Jul 19, 2009 9:24 PM

Reply
3 replies

Jul 19, 2009 10:26 PM in response to halt00

The first thing to keep in mind is the Guidelines are very explicit about programmatic termination. From Stopping in the +iPhone Human Interface Guidelines+:
iPhone applications should never quit programmatically because doing so looks like a crash to the user. There may be times, however, when external circumstances prevent your application from functioning as intended. When this happens, you need to tell users about the situation and explain what they can do about it. This way, users decide whether they want to take corrective action and continue with your application or press the Home button and open a different application.


That said, I guess you didn't post your question to get a lecture on the HIG, so I'll pass on what little I know. I think the alternatives to exit() are \[\[NSThread mainThread\] cancel\] and/or sending \[NSThread exit\] from the main thread. I haven't tried those yet, so dunno if either is any different than exit(). Based on most of what I've read from other developers, the consensus seems to be exit(0), but do your own cleanup just ahead of that. I.e. send your own applicationWillTerminate to the delegate, synchronize any user defaults, etc. I'm guessing nothing essential to the health of the system (like deallocating memory) will fail to happen, so I think you just need to test and find out if you've missed any of your private cleanup.

I'm also guessing I haven't said one thing you didn't already know, but it's the best I can do.

\- Ray

Jul 20, 2009 8:34 PM in response to RayNewbie

For me doing anything with NSThread didn't actually exit the program. The application is left running and is unattended. I monitor the battery level and terminate the app if it drops below 20%. On the next startup and let the user know why the app ended (low battery level). I ended up using exit and calling my cleanup routine before exit and this seems to be clean (no crash logs, etc).

I toyed with doing something like
int* crash = 0;
*crash = 0xBAD;
j/k - well for a second I did - imagine all the crash logs! yikes...

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Programmatic way to terminate iPhone App?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.