Making alarm clock with NSTimer

Posted by Alex G on Stack Overflow See other posts from Stack Overflow or by Alex G
Published on 2012-07-02T09:13:28Z Indexed on 2012/07/02 9:15 UTC
Read the original article Hit count: 163

Filed under:
|
|

I just went through trying to make an alarm clock app with local notifications but that didn't do the trick because I needed an alert to appear instead of it going into notification centre in iOS 5+

So far I've been struggling greatly with nstimer and its functions so I was wondering if anyone could help out.

I want when the user selects a time (through UIDatePicker, only time) for an alert to be displayed at exactly this time. I have figured out how to get the time from UIDatePicker but I do not know how to properly set the firing function of nstimer. This is what I have attempted so far, if anyone could help... be much appreciated. Thank you

Example (it keeps going into the function every second opposed to a certain time I told it too... not what I want):

NSDate *timestamp;
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];

[comps setHour:2];
[comps setMinute:8];
timestamp = [[NSCalendar currentCalendar] dateFromComponents:comps];

NSTimer *f = [[NSTimer alloc] initWithFireDate:timestamp
                             interval:0
                               target:self
                             selector:@selector(test)
                             userInfo:nil repeats:YES];

NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:f forMode: NSDefaultRunLoopMode]; 

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about ios