How To Make iOS App Run At Background Or Exit When Press Home Button

When you press iOS device’s home button, you may want your iOS app go to background and resume running later, or you may want your iOS app terminate and exit at once. This article will tell you how to implement it with example. But first, you had better read article iOS Application Life Cycle Example to have basic learning about iOS app life cycle.

1. Make iOS App Run At Background Or Exit When Press Home Button Example.

  1. Create a Xcode project that coding with swift.
  2. Click the project’s Info.plist file in Xcode left side project navigator pane.
    add ios app icon files in info.plist file
  3. Then click the plus ( + ) icon after Information Property List item at the right pane.
  4. Input and select Application dose not run in background item from the drop down list in the newly added information property item.
  5. Then in the Value column, if you select YES from the value drop down list, that means the iOS app will not run at background and exit when you click home button to suspend it, if you select NO means the iOS app will run at background when you press home button to make it suspended.
    set application do not run at background item value in info.plist file
  6. If you want to see the Raw Keys and Values for above information property item, you can click Editor —> Show Raw Keys & Values menu item at Xcode top menu bar. Then you can see the Application dose not run in background item’s raw key is UIApplicationExitsOnSuspend.
    xcode menu bar editor ---> show raw keys and values
  7. Now add below code in the example project AppDelegate.swift file. It will print a text message in debug console in each of the life cycle method.
    //  AppDelegate.swift
    //  TestProject
    //  Created by song zhao on 8/14/19.
    //  Copyright © 2019 All rights reserved.
    import UIKit
    class AppDelegate: UIResponder, UIApplicationDelegate {
        var window: UIWindow?
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
            print("Start ---> Finish launching.")
            return true
        func applicationWillResignActive(_ application: UIApplication) {
            // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
            // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
            print("Active ---> Inactive.")
        func applicationDidEnterBackground(_ application: UIApplication) {
            // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
            // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
            print("Inactive ---> Background.")
        func applicationWillEnterForeground(_ application: UIApplication) {
            // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
            print("Background ---> Foreground.")
        func applicationDidBecomeActive(_ application: UIApplication) {
            // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
            print("Inactive ---> Active.")
        func applicationWillTerminate(_ application: UIApplication) {
            // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
            print("This app will be terminated.")
  8. Now build and run the iOS example, when you press the home button, you can see the app output log text in Xcode Debug Area —> Activate Console. You can click View —> Debug Area —> Show (Hide) Debug Area , Activate Console menu item to display the console. Please refer article How To Debug And Get Error Messages In Xcode Step By Step.
    Start ---> Finish launching.
    Inactive ---> Active.
    Active ---> Inactive.
    Inactive ---> Background.
  9. If you set Application dose not run in background item’s value to YES, you will get below output text when you press the home button. You can see that the app will terminate when you suspend it at this time.
    Start ---> Finish launching.
    Inactive ---> Active.
    Active ---> Inactive.
    Inactive ---> Background.
    This app will be terminated.
READ :   How To Set Application Root View Controller Programmatically In Xcode 11

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.