Objective-c – xcode – scheduled local notification with time ( Everyday at 6AM )


I'm using Xcode 4.3.2, How can i set this local notification "dateToFire" everyday at 6AM?

    UILocalNotification *localNotification = [[[UILocalNotification alloc] init] autorelease];

    if (!localNotification) 

    // Current date
    NSDate *date = [NSDate date]; 
    NSDate *dateToFire = //Everyday: 6AM;

    // Set the fire date/time
    [localNotification setFireDate:dateToFire];
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
    [localNotification setAlertBody:@"Notification" ];      

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

- (void)viewDidLoad
    [super viewDidLoad];
    [self notification];

Best Solution

Use CalendarComponents to set the hour to 6, and set localNotification.repeatInterval to NSDayCalendarUnit

NSCalendar *calendar = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
[components setHour:18];
[components setMinute:0];

localNotification.fireDate = [calendar dateFromComponents:components];
Related Question