gpt4 book ai didi

ios - 应用进入后台IOS?

转载 作者:行者123 更新时间:2023-11-29 10:36:48 27 4
gpt4 key购买 nike

有一种方法可以在应用程序委托(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{
}

您要添加的通知是:UIApplicationWillTerminateNotificationUIApplicationWillEnterForegroundNotificationUIApplicationDidEnterBackgroundNotification

不要忘记在您类的dealloc 中注销您的类实例,即使在ARC 中也是如此:

-(void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillTerminateNotification object:nil];

}

关于ios - 应用进入后台IOS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26490010/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com