iphone – xcode – 按时间安排本地通知(每天早上6点)
发布时间:2020-12-14 17:49:31 所属栏目:百科 来源:网络整理
导读:我正在使用 Xcode 4.3.2,如何在每天早上6点设置本地通知“dateToFire”? -(void)notification{ UILocalNotification *localNotification = [[[UILocalNotification alloc] init] autorelease]; if (!localNotification) return; // Current date NSDate *da
我正在使用
Xcode 4.3.2,如何在每天早上6点设置本地通知“dateToFire”?
-(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
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]; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |