Jonathan
Jonathan

Reputation: 507

NSNotication at time of day

I need my application to run method at a certain time of day, I think the best way would be to set up a notification for when it would get that time. I have used NSNoticationCenter before but I'm not sure how to set it up for a time of day.

Edit: I am going at it a different way using NSTimer. Here is my code

    NSCalendar* myCalendar = [NSCalendar currentCalendar];
NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
                                             fromDate:[NSDate date]];
[components setHour: 10];
[components setMinute: 5];
[components setSecond: 0];
NSDate *date = [myCalendar dateFromComponents:components];

NSTimer *t = [[NSTimer alloc] initWithFireDate:date
                                      interval:0.0
                                        target:self
                                      selector:@selector(fired:)
                                      userInfo:nil
                                       repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];

Upvotes: 0

Views: 106

Answers (1)

Raviprakash
Raviprakash

Reputation: 2440

Use NSTimer class methods. Create timer object and set fire date accordingly. In the timer callback method implement method to post the required notification.

Alternative way is using performSelector:withObject:afterDelay:. In the selector method post required notification.

Upvotes: 1

Related Questions