0

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.

Víctor Martín
  • 3,352
  • 7
  • 48
  • 94

2 Answers2

3

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:

Community
  • 1
  • 1
Nat
  • 12,032
  • 9
  • 56
  • 103
  • Where I need to put this? In the ViewController? On the viewWillAppear? Sorry for my noob knowledge. – Víctor Martín Jun 30 '15 at 08:43
  • @Victor_J_Martin You should put `addObserver` in your viewController eg in `viewDidLoad`, and `removeObserver` in `dealloc` method. – Nat Jun 30 '15 at 08:45
  • I know, I'm trying to debug my app to view the results, but when the app goes to background the xcode log stops listen, so I don't know at this moment if this works or not. When I see it, I accept the answer. I'm slow, sorry. – Víctor Martín Jun 30 '15 at 10:28
  • @Victor_J_Martin I don't understand. You've asked how to get information if app is going to/from the background (and you should receive it now when you come back from HomeScreen). If you want to see logs in the background it's not relevant to the question you've asked.. You should set proper backgroundMode in this case. – Nat Jun 30 '15 at 12:55
  • I was working to know if your answer solves my problem. I have not accepted the answer because I could not see the result by other issues. But thanks for your help. – Víctor Martín Jul 01 '15 at 06:58
1

iOS is not calling those methods again, but the delegate methods in the AppDelegate. You have to propagate the message to your controllers then.

I hope this will help you: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/

Luca D'Alberti
  • 4,749
  • 3
  • 25
  • 45
  • Then, where I need to call the method [self checkStatus] to be opened everytime app appears? – Víctor Martín Jun 30 '15 at 08:42
  • You have to catch the action in the `AppDelegate` and then you must have a reference to the controller. Or you can use `NSNotification` to send the message to the other components – Luca D'Alberti Jun 30 '15 at 08:43