Search code examples
iosobjective-cviewdidloadviewwillappear

Method in viewWillAppear and viewDidLoad didn't loaded


I have created a method to check the status of a server in my viewcontroller, I need to check this, everytime I will open the app.

I call [self checkStatus]; from viewWillAppear and viewDidLoad, but when I open the app, by clicking home button, and I try to open the app again (clicking the app icon in applications) this method is not called. I have a NSLog to view when it is launched or not.

I'm frustrated. Thanks for your help.


Solution

  • You can react to app changes using NotificationCenter:

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

    BTW: don't forget to removeObserver when you don't need it!

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

    You can also use UIApplicationWillEnterForegroundNotification etc, depending what do you need.

    You should read about app lifecycle on Apple Developer pages :). Read: