Take the 2-minute tour ×

Can anybody say me how to save NSTimer ? I need to display that, at how much time player completed his/her game. please suggest.


share|improve this question
do you mean you wanna know how much time a user has spent for playing a game? –  Andrey Chernukha Dec 4 '12 at 9:37
yes, and after that i have to display that time in leaderboard. –  Abha Dec 4 '12 at 9:38
i just tested my answered code and it's work perfect, i dont know why some one give minus requtation but angel i just req to you just implement my answer code hope its a help you... thx –  Nitin Gohel Dec 4 '12 at 9:56
okat, m trying ur code... –  Abha Dec 4 '12 at 9:58
@Angel thx happy coding.. :) –  Nitin Gohel Dec 4 '12 at 10:07
show 1 more comment

4 Answers

up vote 2 down vote accepted

you can do like this way..

int currentTime;

BOOL isGameOver;

 - (void)viewWillAppear:(BOOL)animated 
       [self start];
       [super viewWillAppear:YES];

- (IBAction)start{

    lbl=[[UILabel alloc]init];
    //creates and fires timer every second
    myTimer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showTime) userInfo:nil repeats:YES]retain];


//your game action Whenever its game over this method fire and Set One Bool value in this method like



            [myTimer invalidate];
            myTimer = nil;
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Game Over"
                                                            message:[NSString stringWithFormat:@"Your Rount Time is %.2d", currentTime]
            [alert show];
            [alert release];

        //you can save this [NSString stringWithFormat:@"Your Rount Time is %.2d", currentTime] in nsuserdefoult or database like bellow

          NSString *myGameLastTime =[NSString stringWithFormat:@"Your Rount Time is %.2d", currentTime] ;
          NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
          [defaults setObject:myGameLastTime forKey:@"gameoverTime"];

          [defaults synchronize];

         //now you can get this time anyplace in your project like [[NSUserDefaults standardUserDefaults] valueForKey:@"gameoverTime"]


        lbl.text = [NSString stringWithFormat:@"%.2d", currentTime];

    NSLog(@"my lable == %@",lbl.text);


Hope its help you all the best

share|improve this answer
add comment

You don't need to use NSTimer. I suggest you using NSDate

When the game starts:

startDate = [NSDate date];

when the game is over:

endDate = [NSDate date];
NSTimeInterval interval = [endDate timeIntervalSinceDate:startDate];

so now you have time spent and can use it however you like. You can save it in NSUserDefaults just like ordinary double value

share|improve this answer
thanksss. its helpful. –  Abha Dec 4 '12 at 10:22
you're very welcome –  Andrey Chernukha Dec 4 '12 at 10:26
add comment

U should do some research before ask such questions, its easy to figure it out with google, it will take something like 20 min max,

NSUserDefaults *YourTimerName=[NSUserDefaults standardUserDefaults];
[defaultsPassword setObject:YourTimerName forKey:@"key1"];
[defaultsPassword synchronize];

and then u can recover it like that

Nsstring *str=[[NSUserDefaults standardUserDefaults]objectForKey:@"key1"];

but you need to make your timer Nsstring format firstly and then save it

share|improve this answer
add comment

NSTimer is just a source of periodic time events, there’s not much to save. If you use it to keep track of time in your game, there has to be a variable that keeps the total time. Save that into NSUserDefaults or somewhere.

share|improve this answer
okay, but i m new in ios so can u say me how to save? i m trying to save time in NSUserDeafults but gives "0" in output. –  Abha Dec 4 '12 at 9:42
add comment

Your Answer


