0

I am making a clock application; with a stopwatch and timer. When my app goes into the background (ie. the user goes onto the homescreen, switches to another app, or locks the phone), the timer and stopwatch stop counting. I know several apps that continue working after it has been put into the background.

Is there a setting I can change to keep it running?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user2397282
  • 3,798
  • 15
  • 48
  • 94
  • 2
    This will help you: [Tutorial](http://www.raywenderlich.com/29948/backgrounding-for-ios) – chris13 Sep 07 '13 at 17:45
  • Your app needs to update the display when it comes from the foreground. Basically keep track of the time it went into the background and update accordingly when it comes into the foreground. – Black Frog Sep 07 '13 at 19:00

4 Answers4

0

Create variable like

self.timeBeforeGoingBackgound = [NSDate date];

Then you can get time using

[[NSDate date] timeIntervalSinceDate:self.timeBeforeGoingBackgound];
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Taier
  • 2,109
  • 12
  • 22
0

The short answer for exact your question is NO You cann't make your application perform some action while it is in background mode.

Here you can find Apple's Doc, which says what can work in background.

Also for your case it is easy to implement stopwatch saving current time for example in NSUserDefaults when this AppDelegate's method applicationDidEnterBackground is called and to update elapsed time in applicationDidBecomeActive method

B.S.
  • 21,660
  • 14
  • 87
  • 109
0

You'll need to save your state when you go into the background and then restore the state when you come back into the foreground. This means both timers and the actually timing data.

Paul Cezanne
  • 8,629
  • 7
  • 59
  • 90
0

Adding code for George's answer:

In your applicationDidEnterBackground

NSDate *currentDate= [NSDate date];
[[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"backgroundDate"];

And in your applicationDidBecomeActive

NSDate *dateWhenAppGoesBg= (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@"backgroundDate"];
NSTimeInterval timeSpentInBackground = [[NSDate date] timeIntervalSinceDate:dateWhenAppGoesBg];

Now you have the timeSpentInBackground in seconds, from which you can update the UI or do the rest of tasks.

HRM
  • 2,097
  • 6
  • 23
  • 37
  • Thanks for the answer, but how do I access `timeSpentInBackground` in my viewController.m file? – user2397282 Sep 08 '13 at 10:04
  • You can post a NSNotification from applicationDidBecomeActive. Please check my answer in this post on how to use this. http://stackoverflow.com/questions/17702022/select-a-row-from-view-when-a-push-notification-is-received/17703073#17703073. – HRM Sep 08 '13 at 10:59
  • I don't quite know how to implement this, what parts go in the AppDelegate, and what parts go in my viewcontroller.m? – user2397282 Sep 08 '13 at 11:15
  • You need to register a notification method in your viewcontroller.m and from applicationDidBecomeActive in ur appDelegate post a notification. So viewcontroller.m notification method will get called and you can use the value there. Is it clear? – HRM Sep 08 '13 at 11:20
  • Kinda, how is the variable being passed? I'm quite confused – user2397282 Sep 08 '13 at 11:29
  • Check out the answer for this question, http://stackoverflow.com/questions/10272068/pass-nsstring-variable-to-other-class-with-nsnotification – HRM Sep 08 '13 at 11:31
  • I get this error when I pass timeSpentInBackground as the object: Sending 'NSTimeInterval' (aka 'double') to parameter of incompatible type 'id' – user2397282 Sep 08 '13 at 11:36
  • You can't pass NSTimeInterval as its a typedef of double. You need to pass it like as NSNumber like this NSNumber *n = [NSNumber numberWithDouble:yourTimeIntervalValue]; – HRM Sep 08 '13 at 11:51