Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

iphone

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?

like image 281
bandw Avatar asked Mar 07 '12 10:03

bandw


People also ask

How do I know if an app is running in the background on iOS?

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.


2 Answers

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
}
like image 115
Matjan Avatar answered Sep 17 '22 13:09

Matjan


applicationWillResignActive:

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

like image 28
Markus Persson Avatar answered Sep 20 '22 13:09

Markus Persson