我正在开发一款iPhone应用程序,在应用程序进入后台之前需要进行一些操作,我知道有applicationWillEnterForeground和applicationDidEnterBackground方法。
但是找不到一个名为applicationWillEnterBackground的通知,有人知道如何做吗?
在viewDidLoad或init中注册此通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
在 Swift 5.0 中
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive(notification:)), name: UIApplication.willResignActiveNotification, object: nil)
@objc func applicationWillResignActive(notification: NSNotification) {
//do a thing
}
在 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:
告诉代理应用程序即将变为非活动状态。