在特定日期触发UILocalNotification [英] Fire UILocalNotification in a specific Date
本文介绍了在特定日期触发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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文