Reputation: 889
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?
Upvotes: 14
Views: 13614
Reputation: 3607
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
}
Upvotes: 23
Reputation: 1093
applicationWillResignActive:
Tells the delegate that the application is about to become inactive.
Upvotes: 1