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?
-
Try looking in your application delegate. – Bamsworld Jan 12 '16 at 13:20
-
3See documentation for `UIApplicationDelegate`. – Phillip Mills Jan 12 '16 at 13:21
-
Does this answer your question? [What's the best way to detect when the app is entering the background for my view?](https://stackoverflow.com/questions/9011868/whats-the-best-way-to-detect-when-the-app-is-entering-the-background-for-my-vie) – Jon Schneider Aug 24 '21 at 14:49
7 Answers
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
}

- 229,809
- 59
- 489
- 571
-
-
-
1this method can be triggered if u receive an SMS too . https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622950-applicationwillresignactive . the method is actually didEnterBackgroundNotification – Jean Raymond Daher May 22 '19 at 14:08
-
1It should be noted that this no longer works for apps under iOS 13 unless the app has opted out of using scenes. – rmaddy Sep 21 '19 at 21:27
-
@rmaddy If so can you please suggest how it should be tackled ? Do I need Xcode 11 for the same ? – Sharad Chauhan Oct 09 '19 at 08:39
-
1@SharadChauhan See https://stackoverflow.com/questions/57488057/view-controller-responds-to-app-delegate-notifications-in-ios-12-but-not-in-ios – rmaddy Oct 09 '19 at 16:14
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")
}

- 3,887
- 1
- 33
- 35
-
1It should be noted that this no longer works for apps under iOS 13 unless the app has opted out of using scenes. – rmaddy Sep 21 '19 at 21:27
Swift3
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: Notification.Name.UIApplicationWillResignActive, object: nil)
func appMovedToBackground() {
print("App moved to background!")
}

- 2,917
- 30
- 28
-
2It should be noted that this no longer works for apps under iOS 13 unless the app has opted out of using scenes. – rmaddy Sep 21 '19 at 21:27
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.

- 2,104
- 16
- 24
-
This would only tell you when the app had already entered the background - there would be some latency between this happening, and the method being called. – Alex Blundell Jan 12 '16 at 13:25
-
1Yes, exactly applicationWillResignActive possible answer, +1 @AlexBlundell : Good work. – Suresh Kumar Durairaj Jan 12 '16 at 13:29
-
It should be noted that this no longer works for apps under iOS 13 unless the app has opted out of using scenes. – rmaddy Sep 21 '19 at 21:28
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!")
}

- 452
- 7
- 18
-
`UIApplication.willResignActiveNotification` is not appropriate for app entering background. It can trigger even when you receive phone call. – Saurabh Prajapati Oct 07 '21 at 12:51
For SwiftUI you can use:
YourView()
.onReceive(NotificationCenter.default.publisher(for: UIScene.willDeactivateNotification)) { _ in
//...
}

- 3,542
- 1
- 34
- 38
-
This is a great answer, you know how can i keep my download when app in background? – Amin Rezaew Jan 24 '23 at 20:27
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

- 2,084
- 5
- 22
- 31
-
It should be noted that this no longer works for apps under iOS 13 unless the app has opted out of using scenes. – rmaddy Sep 21 '19 at 21:27