Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS app Stopwatch and Timer stop counting when in background
    primarykey
    data
    text
    <p>I have an iPhone app which is basically a clock, stopwatch, and timer. Everything works fine, until you leave the app, and the stopwatch and timer stop counting. I think the way to solve this would be to start a counter when the app is put into the background, and add/subtract that from the stopwatch/timer when the app starts up again. Is this the best thing to do, and if so, how would I do this?</p> <p><strong>UPDATE</strong>: The timer, stopwatch, and clock are all in different view controllers. Here is the code for my stopwatch in StopwatchViewController.m:</p> <pre><code>- (void)stopwatch // Start counting the stopwatch { hourInt = [hourLabel.text intValue]; // Store integer values of time minuteInt = [minuteLabel.text intValue]; secondInt = [secondLabel.text intValue]; if (secondInt == 59) { // Add on one second secondInt = 0; if (minuteInt == 59) { minuteInt = 0; if (hourInt == 23) { hourInt = 0; } else { hourInt += 1; } } else { minuteInt += 1; } } else { secondInt += 1; } NSString *hourString = [NSString stringWithFormat:@"%02d", hourInt]; // Update text to show time NSString *minuteString = [NSString stringWithFormat:@"%02d", minuteInt]; NSString *secondString = [NSString stringWithFormat:@"%02d", secondInt]; hourLabel.text = hourString; minuteLabel.text = minuteString; secondLabel.text = secondString; [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(stopwatch) userInfo:nil repeats:NO]; // Repeat every second } </code></pre> <p>How do I get it to add on the elapsed time the app was in the background?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload