本文介绍了在特定日期触发UILocalNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在特定的日期触发UILocalNotification.如果我使用此代码:
I want to fire a UILocalNotification in a specific Date. If I use this code:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]]; [components setHour:4]; [components setMinute:0]; NSDate *fireDate = [gregorian dateFromComponents:components];如果现在是下午3点,则可以正常工作,但例如到下午5点时,则不能正常工作.
If now it's 3 pm this works fine, but it doesn't when, for example, it's 5 pm.
如何将通知触发日期设置为下一个下午4点"?
How can I set the notification fire date to "the next 4 pm" ?
推荐答案NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]]; [components setHour:4]; [components setMinute:0]; NSDate *fireDate = [gregorian dateFromComponents:components]; NSLog(@"Fire date : %@",fireDate);// Today 4pm is already passed. So you wont get notification // You need to check if the time is already passed if ([fireDate compare:[NSDate date]] == NSOrderedAscending) { // Schedule it for next day 4pm components.day = components.day+1; fireDate = [gregorian dateFromComponents:components]; } NSLog(@"Fire date : %@",fireDate);
更多推荐
在特定日期触发UILocalNotification
发布评论