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.
Tells the delegate that the app is about to enter the foreground.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With