Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use UIApplicationDidBecomeActiveNotification

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.

like image 472
Programming Learner Avatar asked Jan 14 '14 06:01

Programming Learner


People also ask

When Applicationwillenterforeground is called?

Tells the delegate that the app is about to enter the foreground.

What is UIApplication in Swift?

Swift version: 5.6. Subclassing UIApplication allows you to override functionality such as opening URLs or changing your icon, but it's a non-trivial task in Swift because of the @UIApplicationMain attribute. If you look in your AppDelegate.


1 Answers

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.

like image 108
malex Avatar answered Sep 23 '22 02:09

malex