How can I use applicationDidBecomeActive in UIViewController?

IphoneObjective CIos

Iphone Problem Overview


I want to reload data in UIViewController when application become active or become foreground.

I know applicationDidBecomeActive is called in AppDelegate class.
But I have to have a global variable for the UIViewController to reload its data in AppDelegate class like this code:

in AppDelegate.m

// global variable
UIViewController *viewController1;
UIViewController *viewController2;

-(void)applicationDidBecomeActive:(UIApplication *)application
{
    [viewController1 reloadData];
    [viewController2 reloadData];
}

But it is inconvenient especially when I have a lot of UIViewControllers.

Can I use applicationDidBecomeActive in UIViewController instead of in AppDelegate class?
Or are there better ways than having global variable for UIViewController?

I also need to use the following method from UIViewControllers:

-(void)applicationWillResignActive:(UIApplication *)application
-(void)applicationDidEnterBackground:(UIApplication *)application
-(void)applicationWillEnterForeground:(UIApplication *)application

Iphone Solutions


Solution 1 - Iphone

At the time of reactivation, if you want to carry a particular thing for a view controller, you should register a notification in its viewDidLoad method.

UIApplicationDidBecomeActiveNotification will automatically notify your application and given controller, if they registered for it.

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

Solution 2 - Iphone

Here is an example of registering a notification handler in Swift (adapted from Apurv's answer above):

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(
        self,
        selector: #selector(applicationDidBecomeActive(notification:)),
        name: NSNotification.Name.UIApplicationDidBecomeActive,
        object: nil)
}

@objc func applicationDidBecomeActive(notification: NSNotification) {
    // do something
}

Update for Swift 4.2:

override func viewDidLoad() {
    super.viewDidLoad()
    
    NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
}

@objc func applicationDidBecomeActive(notification: NSNotification) {
    // Application is back in the foreground
    
    print("active")
}

Solution 3 - Iphone

Swift 3:

NotificationCenter.default.addObserver(
    self,
    selector: #selector(applicationDidBecomeActive(_:)),
    name: NSNotification.Name.UIApplicationDidBecomeActive,
    object: nil)
        
        
            
func applicationDidBecomeActive(_ notification: NSNotification) {
    // do something
}

Note: Don't forget to remove the observer

Solution 4 - Iphone

You cannot use applicationDidBecomeActive in viewController; it is not a method for that class.

However, you can use applicationDidBecomeActive method in AppDelegate to call any methods in your view controller that you feel are important upon launch. Just keep a pointer to your controller so the App Delegate can reach it.

What those methods might be in your view controller is entirely up to you and the details of your program. Maybe it means calling a custom update method in your view controller or whatever else you feel is necessary.

You could also use NSNotificationCenter as outlined here, with many system notifications available for application launch: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html

However, relying heavily on NSNotificationCenter is in my opinion a good way for an app to be come disorganized. If you call everything from your main methods in the AppDelegate only, you can always refer to that method to know exactly what your app is doing upon launch. If instead you use NSNotificationCenter, you could have actions spread across many classes/objects and it can be harder to track down what is going on. Since you mentioned multiple controller objects, I think it is more streamlined and organized to call everything from applicationDidBecomeActive rather than register each viewcontroller for the same notification.

Solution 5 - Iphone

update for swift 5

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)

    }
   // remove the observer 
   deinit {

       print("Receiver teardown")

       NotificationCenter.default.removeObserver(self)

   }

    @objc func applicationDidBecomeActive(notification: NSNotification) {
        // Application is back in the foreground

        print("applicationDidBecomeActive")
    }

}

Solution 6 - Iphone

Thank you all for answering my question.
But I found easier way to use applicationDidBecomeActive in UIViewController.

@implementation AppDelegate

-(void)applicationDidBecomeActive:(UIApplication *)application
{
    UIViewController<MyAppDelegate> *topViewController = (UIViewController<MyAppDelegate> *)navigationController.topViewController;
    if ([topViewController respondsToSelector:@selector(MyApplicationDidBecomeActive)]) {
        [topViewController MyApplicationDidBecomeActive];
    }
}
@end

@protocol MyAppDelegate
@optional
-(void)MyApplicationDidBecomeActive;
@end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjs_View Question on Stackoverflow
Solution 1 - IphoneApurvView Answer on Stackoverflow
Solution 2 - IphoneAdrian MacneilView Answer on Stackoverflow
Solution 3 - IphoneDaniView Answer on Stackoverflow
Solution 4 - IphonejohnbakersView Answer on Stackoverflow
Solution 5 - IphoneZgpeaceView Answer on Stackoverflow
Solution 6 - Iphonejs_View Answer on Stackoverflow