Register iPhone to update your watch

My application has a clock that I would like to synchronize with the system. A number of stack questions are centered around NSTimer, but before that I want to check if there is a system notification that I can register that runs every minute.

Is there such a thing? I am browsing NSNotificationCenter but have nothing so far.

+5
source share
4 answers

You can use NSTimer initWithFireDatefor shooting by the minute, calculating the following change of minute:

NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"ss"];
int currentTimeSeconds = [dateFormatter stringFromDate:currentDate].intValue;

NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:60 - currentTimeSeconds];
NSTimer *updateTimer = [[NSTimer alloc] initWithFireDate:fireDate
                                                 interval:60
                                                   target:self
                                                 selector:@selector(updateSelector)
                                                 userInfo:nil
                                                  repeats:YES];

NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:updateTimer forMode:NSDefaultRunLoopMode];
+9
source

NSNotificationCenter, . , NSTimer. NSTimer 1 , . Time/Date .

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self 
                 selector:@selector(clockDateAndTime:) userInfo:nil repeats:YES];

-(void)clockDateAndTime:(NSTimer*)timer 
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    NSDate * date = [NSDate date];
    [formatter setDateFormat:@"MMM dd, yyyy hh:mm:ss a"];
    [lblLocalDate setText:[formatter stringFromDate:date]];
}
+2

Beggs answer is higher right ... here it is for quick:

let dateComp = self.calendar.components(NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.MonthCalendarUnit | NSCalendarUnit.YearCalendarUnit | NSCalendarUnit.CalendarUnitHour | NSCalendarUnit.CalendarUnitMinute | NSCalendarUnit.CalendarUnitSecond, fromDate: NSDate())
    dateComp.minute += 1
    dateComp.second = 0

    println(self.calendar.dateFromComponents(dateComp)!)
    let myTimer =  NSTimer(fireDate: self.calendar.dateFromComponents(dateComp)!, interval: 60, target: self, selector: "refreshCountdownNextMeal", userInfo: nil, repeats: true)
    let runloop = NSRunLoop.currentRunLoop()
    runloop.addTimer(myTimer, forMode: NSDefaultRunLoopMode)

I do not need to tell you that calling a timer once a minute is better than 60 times a minute ?; -)

0
source

More literal quick 2 translation of @beggs answer:

let currentDate = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "ss";
let currentTimeSeconds = Double(dateFormatter.stringFromDate(currentDate))
let fireDate = NSDate(timeIntervalSinceNow: 60 - currentTimeSeconds!)

let timer = NSTimer(fireDate: fireDate, interval: 60, target: self, selector: "minuteLoop", userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)
0
source

All Articles