Is there a way of tracking the amount of time the app is in background and display the same when the app enters foreground in iOS?
Asked
Active
Viewed 717 times
0
-
Is "in background" for you different than "terminated"? From your question I'd guess so and that you want to show the time it took for your app to be terminated since it entered background. – Fábio Oliveira Feb 13 '13 at 19:31
-
Yeah sort of, What time does it take for an app to be terminated from it enters background state? Any hints as to how to know this? – user2070420 Feb 13 '13 at 19:33
-
Are you saying that you want to know how long the app was in the background before it was terminated? Your comment along with the comment to the answer already given seems to contradict the original question. – ChrisH Feb 13 '13 at 19:48
-
It contradicts based on the fact that it was accepted since it doesn't work for what the OP wants. – Fábio Oliveira Feb 13 '13 at 22:05
1 Answers
5
A simple way could be to save the date into NSUserDefaults in your app delegate's applicationDidEnterBackground:
method, then compare the date from the defaults to the current date in your applicationWillEnterForeground:
method. Would that work for you?

Stuart Sharpe
- 601
- 5
- 8
-
No, I need the exact time, If there is a way I am planning to implement some methods based on the time the app is in background – user2070420 Feb 13 '13 at 19:29
-
@user1886224 in what sense would this approach lead to an inexact result? – Tommy Feb 13 '13 at 19:45
-
`-[NSDate timeIntervalSinceDate:]` would give you a precise time interval in seconds between the two dates, which sounds like what you're asking for. The user defaults value should persist even if the app is terminated. – Stuart Sharpe Feb 13 '13 at 20:05
-
Use the time since these two events: application:didEnterBackground: and application:willTerminate: or application:willEnterForeground. Check this answer (http://stackoverflow.com/a/3139649/1130894) for when the willTerminate is called. – Fábio Oliveira Feb 13 '13 at 22:12