Im developing an iPhone App, need to do something before application did enter background, I know there are applicationWillEnterForeground and applicationDidEnterBackground
But can't find a application*Will*EnterBackground notification, anyone know how to do that?
To detect if an iOS application is in background or foreground we can simply use the UIApplication just like we can use it to detect many other things like battery state, status etc. The shared. application state is an enum of type State, which consists of the following as per apple documentation.
Register for this notification in viewDidLoad or at init:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
In Swift 5.0
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive(notification:)), name: UIApplication.willResignActiveNotification, object: nil)
@objc func applicationWillResignActive(notification: NSNotification) {
//do a thing
}
In Swift 4.0
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive(notification:)), name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
@objc func applicationWillResignActive(notification: NSNotification) {
//do a thing
}
applicationWillResignActive:
Tells the delegate that the application is about to become inactive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With