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

查看:17
本文介绍了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];

实现一个需要调用的代码:

Implement a code, that need to be called:

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

别忘了退订:

[[NSNotificationCenter defaultCenter] removeObserver:self];

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

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