有一种方法可以在应用程序委托(delegate)的方法“applicationDidEnterBackground”被调用但只是获取应用程序对象时通知对象。
我需要在应用程序进入后台时执行一些操作,但我只能通过“[UIApplication sharedApplication]”访问应用程序对象。
注意:我需要这 3 个方法 applicationWillTerminate、applicationWillEnterForeground、applicationDidEnterBackground,但我无法访问 applicationDelegate 方法。
最佳答案
您可以使用 NSNotificationCenter
通知您的类这些方法正在被调用。
在 init 中注册正确的通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminateNotification:) name:UIApplicationWillTerminateNotification object:nil];
这将调用一个方法这个方法,你必须在你的类中添加它:
- (void)applicationWillTerminateNotification::(NSNotification *)notifictaion{
}
您要添加的通知是:UIApplicationWillTerminateNotification
、UIApplicationWillEnterForegroundNotification
和 UIApplicationDidEnterBackgroundNotification
不要忘记在您类的dealloc
中注销您的类实例,即使在ARC 中也是如此:
-(void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillTerminateNotification object:nil];
}
关于ios - 应用进入后台IOS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26490010/