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?
Have you tried exit(0)
?
Alternatively, [[NSThread mainThread] exit]
, although I have not tried that it seems like the more appropriate solution.
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.
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.
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:
if ([[UIApplication sharedApplication] respondsToSelector:@selector(terminate)]) {
[[UIApplication sharedApplication] performSelector:@selector(terminate)];
} else {
kill(getpid(), SIGINT);
}
After some tests, I can say the following:
- 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);
.
Its not really a way to quit the program, but a way to force people to quit.
UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:@"Hit Home Button to Exit" message:@"Tell em why they're quiting" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[anAlert show];
Hi,
[[UIApplication sharedApplication] terminateWithSuccess];
It worked fine and automatically calls - (void)applicationWillTerminateUIApplication *)application delegate.
to remove compile time warning add this code @interface UIApplication(MyExtras) - (void)terminateWithSuccess; @end
to remove compile time warning add this code @interface UIApplication(MyExtras) - (void)terminateWithSuccess; @end
Oh yeah, using an undocumented interface -- great idea. That's always going to work in future versions of the SDK, right?
Oh yeah, using an undocumented interface -- great idea. That's always going to work in future versions of the SDK, right?
Yes, and since there are no any recommended way, let's use "-(int) terminate {return 1/0;}" instead - it will work in ALL future versions, seriously.
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"
Check the QA here.
http://developer.apple.com/iphone/library/qa/qa2008/qa1561.html
From the Apple docs: "Applications calling exit will appear to the user to have crashed" Sorry, I call BS on this one. What if I put up an alert and say:
"You sure you want to quit?" -> "Yes" - "No"
And then quit based on key hit. This is how applications have been quitting since forever. Apple is all of a sudden smarter than that? Give me a break!
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.
Are you ok with displaying "more apps from us" or something like that when ur app quits ?
If so, you can launch a URL :) whenever you want to quit the app. The app quits and opens up safari. Apple is not going to stop you from doing that :)