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

前端 未结 7 959
我在风中等你
我在风中等你 2020-12-07 00:41

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 ap

7条回答
  •  春和景丽
    2020-12-07 01:09

    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];
    

提交回复
热议问题