iOS NSNotificationCenter to check whether the app came from background to foreground

user3115014 picture user3115014 · Jul 23, 2014 · Viewed 29.9k times · Source

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.

Answer

Nikita Took picture Nikita Took · Jul 23, 2014

Have you tried UIApplicationWillEnterForegroundNotification?

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

Subscribe to notification:

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

Implement a code, that need to be called:

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

Don't forget to unsubscribe:

[[NSNotificationCenter defaultCenter] removeObserver:self];