shebi
shebi

Reputation: 719

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?

-(void)notification
{
    UILocalNotification *localNotification = [[[UILocalNotification alloc] init] autorelease];

    if (!localNotification) 
        return;

    // 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];
}

Upvotes: 4

Views: 5918

Answers (1)

Nikita Pestrov
Nikita Pestrov

Reputation: 5966

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];

Upvotes: 6

Related Questions