0

I want to get current device time in my app,and continuously update that time when my device time is update.i follow that code

     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"dd/mm/yy hh:mm"];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    NSString*  todayString = [dateFormatter stringFromDate:[NSDate date]];

I get the current device time,but its not update automatically when user use my app. what can i do.

Vin
  • 10,517
  • 10
  • 58
  • 71
Shashi Sharma
  • 126
  • 1
  • 11

1 Answers1

2

The todayString has to change as device time changes. That means, you need to create a timer of 60 seconds, and update time for every 60 seconds.

You need to do it like this:

NSTimer *timer = [NSTimer timerWithTimeInterval:60.0 target:self selector:@selector(updateTimer:)
                                                   userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

- (void) updateTimer:(id) object
{
  // Write required code here.
}
mah
  • 39,056
  • 9
  • 76
  • 93