iOS NSNotificationCenter用于检查应用程序是否从后台到前台 [英] iOS NSNotificationCenter to check whether the app came from background to foreground

查看:206
本文介绍了iOS NSNotificationCenter用于检查应用程序是否从后台到前台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我必须每次从背景到前景初始化一个对象,并且应该使用NSNotificationCenter而不是appdelegate,因为我建立了一个静态库,因此不会有appdelegate,所以请帮助我同样。

I have a situation in which i have to intialize an object everytime when it comes from background to foreground and that should be using the NSNotificationCenter not with appdelegate because iam building a static library so there wont be appdelegate with that so please help me in the same.

推荐答案

你试过 UIApplicationWillEnterForegroundNotification

该应用程序还会在调用 applicationWillEnterForeground:之前不久发布UIApplicationWillEnterForegroundNotification通知,以便为感兴趣的对象提供响应转换的机会。

The app also posts a UIApplicationWillEnterForegroundNotification notification shortly before calling applicationWillEnterForeground: to give interested objects a chance to respond to the transition.

订阅通知:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(yourUpdateMethodGoesHere:)
                                             name:UIApplicationWillEnterForegroundNotification
                                           object:nil];

实施需要调用的代码:

- (void) yourUpdateMethodGoesHere:(NSNotification *) note {
// code
}

不要忘记取消订阅:

[[NSNotificationCenter defaultCenter] removeObserver:self];

这篇关于iOS NSNotificationCenter用于检查应用程序是否从后台到前台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆