bandw
bandw

Reputation: 889

Is there any notification to get before application did enter background?

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

Answers (2)

Matjan
Matjan

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

Markus Persson
Markus Persson

Reputation: 1093

applicationWillResignActive:

Tells the delegate that the application is about to become inactive.

Upvotes: 1

Related Questions