Reputation: 16813
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?
Upvotes: 2
Views: 770
Reputation: 56362
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.
Upvotes: 4
Reputation: 94
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
Upvotes: 0
Reputation: 53561
Implement applicationDidBecomeActive:
in your app delegate or observe the UIApplicationDidBecomeActiveNotification
notification.
Upvotes: 2