-1

I want to resume time counter value in when I start the app.like first my label value is 05:00 than after close the app but when I start the app agin that time timer count start to 05:00. Please help me

int timeSec,timeMin ; 
- (void)viewDidLoad {
    timeSec=0;
    timeMin=0;
    [self StartTimer];
}
-(void) StartTimer
{
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];


}

//Event called every time the NSTimer ticks.
- (void)timerTick:(NSTimer *)timer
{
    timeSec++;
    if (timeSec == 60)
    {
        timeSec = 0;
        timeMin++;
    }
    //Format the string 00:00
    NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
    //Display on your label
    //[timeLabel setStringValue:timeNow];
    self.lbl_timer.text= timeNow;
}

//Call this to stop the timer event(could use as a 'Pause' or 'Reset')
- (void) StopTimer
{
   // [timer invalidate];
    timeSec = 0;
    timeMin = 0;
    //Since we reset here, and timerTick won't update your label again, we need to refresh it again.
    //Format the string in 00:00
    NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
    //Display on your label
    // [timeLabel setStringValue:timeNow];
    self.lbl_timer.text= timeNow;
}

thanks for advance..

Himanth
  • 2,381
  • 3
  • 28
  • 41
iosdv
  • 38
  • 7
  • 1
    Save the timer start time somewhere in persistent storage (e.g. `NSUserDefaults`, Core Data, SQLite, etc.). Then, we then app restarts, recover that start time, and use that as the basis for showing the amount of time elapsed. It's Swift, but this illustrates some of the key issues: http://stackoverflow.com/questions/34496389/swift-nstimer-in-background/34497360#34497360 – Rob Dec 30 '16 at 10:28
  • then again when you want to reset your time ? – vaibhav Dec 30 '16 at 10:37
  • To which value do you want to set the timer? To the value it had when the app was stopped, or one computed taken into consideration the duration between app sessions? – Cristik Dec 30 '16 at 10:50

2 Answers2

1

Try this code.store data in nsuserdefault

- (void)viewDidLoad {

    timeSec=[[NSString stringWithFormat:@"%ld",(long)[[NSUserDefaults       standardUserDefaults]integerForKey:@"timeSec"]] intValue];
    timeMin=[[NSString stringWithFormat:@"%ld",(long)[[NSUserDefaults standardUserDefaults]integerForKey:@"timeMin"]] intValue];
    [self StartTimer];
}
-(void) StartTimer
{
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    self.lbl_timer.text=@"00:00";

}
- (void)timerTick:(NSTimer *)timer
{
    timeSec++;
    if (timeSec == 60)
    {
        [locationManager startUpdatingLocation];
        timeSec = 0;
        timeMin++;
    }
    self.lbl_timer.text= [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];;
}

when you back to the screen that time add this code.like back button or close button action

[timer invalidate];
                                       [[NSUserDefaults standardUserDefaults]setInteger:timeSec forKey:@"timeSec"];
                                       [[NSUserDefaults standardUserDefaults]setInteger:timeMin forKey:@"timeMin"];
Jigar
  • 1,801
  • 1
  • 14
  • 29
0

Declare two variable in appdelegate file

1)NSUserDefaults *usedefaults; 2)@property (nonatomic,readwrite) int timeSec,timeMin,isBackground;

- (void)applicationDidEnterBackground:(UIApplication *)application 
{
     self.isBackground=1;
     usedefaults=[NSUserDefaults standardUserDefaults];
    [usedefaults setObject:[NSString stringWithFormat:@"%d",self.timeMin] forKey:@"timemin"];
    [usedefaults setObject:[NSString stringWithFormat:@"%d",self.timeSec] forKey:@"timesec"];
    NSLog(@"Enter in back value userdefault %@",usedefaults);
}

- (void)applicationWillEnterForeground:(UIApplication *)application {

    if(self.isBackground==1)
    {
        self.isBackground=0;
        self.timeMin=[[usedefaults objectForKey:@"timemin"] intValue];
        self.timeSec=[[usedefaults objectForKey:@"timesec"] intValue];
        NSLog(@"Foreground value min %d sec %d",self.timeMin,self.timeSec);
    }
}

In ViewDidload in your Timer startpage
app=(AppDelegate *)[[UIApplication sharedApplication] delegate];

finally just replace you self.timemin to app.timemin same for timesec.

Cheers enjoy That surely work for u ..