xcode - scheduled local notification with time ( Everyday at 6AM )
我使用的是 Xcode 4.3.2,我如何在每天早上 6 点设置此本地通知"dateToFire"?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | -(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]; } |
使用 CalendarComponents 将小时设置为 6,并将 localNotification.repeatInterval 设置为 NSDayCalendarUnit
1 2 3 4 5 6 | 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]; |