Proper way to exit iPhone application?

2018-12-31 04:43发布

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条回答
余生无你
2楼-- · 2018-12-31 05:30

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

-(IBAction)doExit
{
    //show confirmation message to user
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Confirmation"
                                                 message:@"Do you want to exit?"
                                                delegate:self
                                       cancelButtonTitle:@"Cancel"
                                       otherButtonTitles:@"OK", nil];
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != 0)  // 0 == the cancel button
    {
        //home button press programmatically
        UIApplication *app = [UIApplication sharedApplication];
        [app performSelector:@selector(suspend)];

        //wait 2 seconds while app is going background
        [NSThread sleepForTimeInterval:2.0];

        //exit app when app is in background
        exit(0);
    }
}
查看更多
浮光初槿花落
3楼-- · 2018-12-31 05:30

add UIApplicationExitsOnSuspend property on application-info.plist to true

查看更多
高级女魔头
4楼-- · 2018-12-31 05:30

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.

查看更多
怪性笑人.
5楼-- · 2018-12-31 05:30
[[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 
查看更多
永恒的永恒
6楼-- · 2018-12-31 05:31

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.

查看更多
十年一品温如言
7楼-- · 2018-12-31 05:31

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.

Why? Because my app uses certificate pinning on all network API calls to prevent man-in-the-middle attacks. These include the initialization calls my financial app makes on startup.

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.

查看更多
登录 后发表回答