我不明白为什么LocalNotification
能够与NSHourCalendarUnit完美配合,但永远不会使用NSWeekCalendarUnit
和NSDayCalendarUnit
,尽管它会在日志中显示正确的时间!!当设置weekday
我需要它时,必须有一些我遗漏的东西..它会是什么?
请查看日志出来的代码,以便指导我正在做的错误:
-(void) Monday { // 9th Notification Every 1:35
NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
NSDateComponents *components = [calendar components: NSDayCalendarUnit | NSYearCalendarUnit | NSWeekCalendarUnit fromDate:[NSDate date]];
components.weekday = 2;
components.hour = 10;
components.minute = 56;
components.second = 35;
NSDate *fire = [calendar dateFromComponents:components];
NSLog(@"The Fire Day is:: %@", fire);
UILocalNotification *notif = [[UILocalNotification alloc] init] ;
if (notif == nil)
return;
notif.fireDate = fire;
notif.repeatInterval= NSWeekCalendarUnit ;
notif.soundName = @"ring.wav";
notif.alertBody = [NSString stringWithFormat: @"THANKS for Your HELP :)"] ;
[[UIApplication sharedApplication] scheduleLocalNotification:notif] ;
NSLog(@"*****NEW LOG Notification Details*****%@", notif);
}
这是日志:
2014-01-20 10:09:12.935 daily notification test[17591:c07] The Fire Day is:: 2014-01-20 10:10:35 +0000
* 日志更新: *
2014-01-20 10:56:21.694 daily notification test[17823:c07] The Fire Day is:: 2014-01-20 10:56:35 +0000
2014-01-20 10:56:21.697 daily notification test[17823:c07] *****NEW LOG Notification Details*****<UIConcreteLocalNotification: 0x7180360>{fire date = Monday, January 20, 2014, 11:56:35 AM Central European Standard Time, time zone = (null), repeat interval = NSWeekCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Monday, January 20, 2014, 11:56:35 AM Central European Standard Time, user info = (null)}
.
2014-01-20 10:56:21.694每日通知测试[17823:c07]消防日是:: 2014-01-20 10:56:35 +0000 2014-01-20 10:56:21.697每日通知测试[17823:c07] ** 新日志通知详情 ** {开火日期= 2014年1月20日星期一,11:56:35 AM中欧时间标准时间,时区=(null),重复间隔= NSWeekCalendarUnit,重复计数= UILocalNotificationInfiniteRepeatCount,下次开火日期= 2014年1月20日星期一,上午11:56:35中欧时间标准时间,用户信息=(null)}
答案 0 :(得分:1)
只需替换
notif.repeatInterval= NSHourCalendarUnit ;
带
notif.repeatInterval= NSWeekCalendarUnit;
已编辑
现在试试这个,这可能有帮助
NSCalendar *gregorian1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian1 setLocale:[NSLocale currentLocale]];
NSDateComponents *nowComponents = [gregorian1 components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate new]];
[nowComponents setWeekday:2]; //Monday
[nowComponents setWeek: [nowComponents week] + 1]; //Next week
[nowComponents setHour:8]; //8a.m.
[nowComponents setMinute:0];
[nowComponents setSecond:0];
NSDate *beginningOfWeek = [gregorian1 dateFromComponents:nowComponents];
答案 1 :(得分:0)
您无法在weekday
中设置NSDateComponents
而无需调整day
因此。这个计算应该有效:
NSInteger wantedWeekday = 2; // Monday
NSDateComponents *components = [calendar components: NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit
fromDate:[NSDate date]];
components.day += wantedWeekday - components.weekday;
components.weekday = wantedWeekday;
components.hour = 8;
components.minute = 10;
components.second = 35;
NSDate *fire = [calendar dateFromComponents:components];
这会计算上一个或下一个星期一的日期,但事实并非如此
物。如果fire
过去,UILocalNotification
将自动更正。
备注: NSLog(@"%@", aNotification)
显示计算的下一个开火日期
UILocalNotification
。这有助于诊断问题。