How to use UIApplicationDidBecomeActiveNotification

cocoa-touch, ios, objective-c

Solution

Sometimes it is useful to have a listener of UIApplicationDidBecomeActiveNotification when you need to make some action in your view controller on wake up from background (in case you entered to background with this view controller on-screen). In such wake up viewWillAppear will not be triggered!

Example of use:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(someMethod)     name:UIApplicationDidBecomeActiveNotification object:nil];
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];

}

- (void)someMethod
{
    <YOUR CODE AT WAKE UP FROM BACKGROUND>
}

Of course, you can also implement all you need at your app delegate class life cycle.

Problem

How to use `UIApplicationDidBecomeActiveNotification`? Should I declare it in `viewDidLoad` or `viewWillAppear` to reload data when coming from background to foreground. Does `UIApplicationDidBecomeActiveNotification` gets called only when app comes from background to foreground? Please help. Thanks.

Original source