Call a UIViewController method when application goes background and come to foreground

2.3k views Asked by At

how to call a method when application goes background and changes some activity like the time or date of mobile and come to foreground (application active mode).

Then i want to call a method in UIViewController classes

FirstViewController Class Method.

-(void)refreshItems{
// Your item refresh code.
}
4

There are 4 answers

0
user5761756 On

I guess you just need

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

0
Birendra On
you have set notification in appDelegate class

    - (void)applicationWillEnterForeground:(UIApplication *)application {

        [[NSNotificationCenter defaultCenter] postNotificationName:@"forground"      object:nil];

    }

and add observer to your viewcontroller class viewdidload() methos

     [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(showMainMenu:)
                                                     name:@"refreshItems" object:nil];
1
Vinupriya Arivazhagan On

Check this code (if FirstViewController is window's rootViewController else take instance of FirstViewController in AppDelegate and check null)

func applicationDidEnterBackground(_ application: UIApplication) {
    if let appDelegate = UIApplication.shared.delegate as? AppDelegate,
        let controller = appDelegate.window?.rootViewController as? FirstViewController {
        controller.refreshItems()
    }
}

func applicationWillEnterForeground(_ application: UIApplication) {
    if let appDelegate = UIApplication.shared.delegate as? AppDelegate,
        let controller = appDelegate.window?.rootViewController as? FirstViewController {
        controller.refreshItems()
    }
}

in Objective-C

- (void)applicationDidEnterBackground:(UIApplication *)application {
   AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
   FirstViewController *controller = (FirstViewController*)[[appDelegate window] rootViewController];
   [controller refreshItems];
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    FirstViewController *controller = (FirstViewController*)[[appDelegate window] rootViewController];
    [controller refreshItems];
}
0
ShigaSuresh On

Add Below Code in ViewController

 override func viewDidLoad() {
 super.viewDidLoad()

//Mark:- application move to bckground

 let backgorundNotificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector:#selector(appMovedToBackground), name: UIApplication.willResignActiveNotification, object: nil)

//Mark:- application move to foreground

let foregroundNotificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector:#selector(appMovedToForeground),name: UIApplication.willEnterForegroundNotification, object: nil)

 }

//Mark:- background method implementation

@objc func appMovedToBackground() {
print("App moved to background!")
 }

 //Mark:- foreground method implementation

@objc func appMovedToForeground() {
   print("App moved to foreground!")
}