Detect iOS app entering background

IosIphoneSwift

Ios Problem Overview


I'm working on a game for iOS coded in Swift. I've tried to find a way to detect when the app enters background mode or is interrupted for other reasons, for example a phone call but can't find anything. How do I do it?

Ios Solutions


Solution 1 - Ios

You can add an observer to your view controller:

edit/update: Xcode 11 • Swift 5

iOS13 or later

UIScene.willDeactivateNotification

iOS12 or earlier

UIApplication.willResignActiveNotification

if #available(iOS 13.0, *) {
    NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: UIScene.willDeactivateNotification, object: nil)
} else {
    NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: UIApplication.willResignActiveNotification, object: nil)
}

and add a selector method to your view controller that will be executed when your app receives that notification:

@objc func willResignActive(_ notification: Notification) {
    // code to execute
}

Solution 2 - Ios

In swift 5.x: To observe app enters background event, add this code to your viewDidLoad() method.

    let notificationCenter = NotificationCenter.default
    notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)

    @objc func appMovedToBackground() {
        // do whatever event you want
    }

you have to use UIApplication.didEnterBackgroundNotification. If you want to observe if app came to foreground event, use UIApplication.willEnterForegroundNotification

So, the full code will be:

override func viewDidLoad() {
    super.viewDidLoad()

    let notificationCenter = NotificationCenter.default
    notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
    
    notificationCenter.addObserver(self, selector: #selector(appCameToForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
    
    // Do any additional setup after loading the view.
}
 @objc func appMovedToBackground() {
    print("app enters background")
}

@objc func appCameToForeground() {
    print("app enters foreground")
}

Solution 3 - Ios

Swift3

let notificationCenter = NotificationCenter.default
    notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: Notification.Name.UIApplicationWillResignActive, object: nil)


func appMovedToBackground() {
    print("App moved to background!")
}

Solution 4 - Ios

To detect the app enters background, you can check in the appDelegate.m find the application delegate method

> applicationDidEnterBackground

This method will get called, once the app enters background.

Solution 5 - Ios

SwiftUI

From background

Text("Hello, World!")
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
    print("To the foreground!")
}

To the background

Text("Hello, World!")
    .onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { _ in
        print("To the background!")
    }

Solution 6 - Ios

For SwiftUI you can use:

YourView()
 .onReceive(NotificationCenter.default.publisher(for: UIScene.willDeactivateNotification)) { _ in
     //...
 }

Solution 7 - Ios

Take a look at the delegate methods defined in your instance of UIApplicationDeletegate (called AppDelegate.m by default). Specifically the following would be useful:

- (void)applicationWillResignActive:(UIApplication *)application

> This method is called to let your app know that it is about to move from the 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 app and it begins the transition to the background state. An app in the inactive state continues to run but does not dispatch incoming events to responders.

Taken from the Apple Documentation - here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohannes FloodView Question on Stackoverflow
Solution 1 - IosLeo DabusView Answer on Stackoverflow
Solution 2 - IosRubaiyat Jahan MumuView Answer on Stackoverflow
Solution 3 - IosdimohamdyView Answer on Stackoverflow
Solution 4 - IosSuresh Kumar DurairajView Answer on Stackoverflow
Solution 5 - IosKarmadonView Answer on Stackoverflow
Solution 6 - IosAmir KhorsandiView Answer on Stackoverflow
Solution 7 - IosAlex BlundellView Answer on Stackoverflow