2

I would like to record a timestamp whenever the user opens the app. Now, I was able to record in the ViewDidLoad method, however, once the user closes the app and it goes to the background and then open the app once more, I can't get the timestamp, because ViewDidLoad doesn't run anymore. Does any of you have any idea where to put my timestamp code?

Cezar
  • 55,636
  • 19
  • 86
  • 87
casillas
  • 16,351
  • 19
  • 115
  • 215
  • Use viewWillAppear instead. http://stackoverflow.com/questions/3643559/what-uiviewcontroller-method-is-called-when-opening-app-from-background – Madivad Jul 28 '13 at 00:44
  • `viewWillAppear:` will [not get called](http://stackoverflow.com/questions/5277940/why-does-viewwillappear-not-get-called-when-an-app-comes-back-from-the-background) when the application enters the foreground. – omz Jul 28 '13 at 00:48

3 Answers3

4

You could put it inside - (void)applicationDidBecomeActive:(UIApplication *)application , but it isn't a good approach, since the user starting the app isn't the only activity that will cause the timestamp to be saved, as there are other situations when this method might get called.

From Apple's Documentation

applicationDidBecomeActive: Tells the delegate that the application has become active.

This method is called to let your application know that it moved from the inactive to active state. This can occur because your application was launched by the user or the system. Applications can also return to the active state if the user chooses to ignore an interruption (such as an incoming phone call or SMS message) that sent the application temporarily to the inactive state.

The correct approach is to put the code that stores the timestamp both inside application:didFinishLaunchingWithOptions: and applicationWillEnterForeground: The first method will get called on the first launch and the second one when the application is launched from the background. Since an ignored interruption wouldn't send the app to the background, applicationWillEnterForeground would not get called in this case, so you wouldn't have a timestamp stored after the user ignores a phone call for example.

Check this answer, since it very well summarizes the role of the methods involved in handling sending an app to the background and getting it back to the foreground and what should you use each of them for.

Community
  • 1
  • 1
Cezar
  • 55,636
  • 19
  • 86
  • 87
2

Implement applicationDidBecomeActive: in your app delegate or observe the UIApplicationDidBecomeActiveNotification notification.

omz
  • 53,243
  • 5
  • 129
  • 141
0

Try using - (void)viewDidAppear:(BOOL)animated: instead. It is called whenever the view appears on the screen.

Or you can look into the UIApplicationDelegate and try implementing something like:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

or

- (void)applicationDidBecomeActive:(UIApplication *)application

Cezar
  • 55,636
  • 19
  • 86
  • 87
yliu1021
  • 94
  • 5