Proper way to exit iPhone application?

前端 未结 25 2687
难免孤独
难免孤独 2020-11-22 01:54

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 ca

相关标签:
25条回答
  • 2020-11-22 02:49

    Have you tried exit(0)?

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

    0 讨论(0)
  • 2020-11-22 02:51

    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:

    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
        if (/* logged out */) {
            exit(0);
        } else {
           // normal handling.
        }
    }
    

    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.

    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
        if (/* logged out */) {
           // stop requesting location updates if not already done so
           // tidy up as app will soon be terminated (run a background task using beginBackgroundTaskWithExpirationHandler if needed).
        } else {
           // normal handling.
        }
    }
    

    And of course, using exit(0) would never be appropriate for the average production app that runs in the foreground, as per other answers that reference http://developer.apple.com/iphone/library/qa/qa2008/qa1561.html

    0 讨论(0)
  • 2020-11-22 02:52

    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.

    0 讨论(0)
  • 2020-11-22 02:53

    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.

    0 讨论(0)
  • 2020-11-22 02:53

    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)).

    0 讨论(0)
  • 2020-11-22 02:55

    You should not directly call the function exit(0) as it will quit the application immediately and will look like your app is crashed. So better to show users a confirmation alert and let them do this themselves.

    Swift 4.2

    func askForQuit(_ completion:@escaping (_ canQuit: Bool) -> Void) {
        let alert = UIAlertController(title: "Confirmation!", message: "Do you want to quit the application", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Yes", style: UIAlertAction.Style.default, handler: { (action) in
            alert.dismiss(animated: true, completion: nil)
            completion(true)
        }))
        alert.addAction(UIAlertAction(title: "No", style: UIAlertAction.Style.cancel, handler: { (action) in
            alert.dismiss(animated: true, completion: nil)
            completion(false)
        }))
        self.present(alert, animated: true, completion: nil)
    }
    
    /// Will quit the application with animation
    func quit() {
        UIApplication.shared.perform(#selector(NSXPCConnection.suspend))
        /// Sleep for a while to let the app goes in background
        sleep(2)
        exit(0)
    }
    

    Usage:

    self.askForQuit { (canQuit) in
         if canQuit {
             self.quit()
         }
    }
    
    0 讨论(0)
提交回复
热议问题