- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我不明白为什么 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 +00002014-01-20 10:56:21.697 每日通知测试[17823:c07] **NEW LOG 通知详情**{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 中欧标准时间, 用户信息 = (null)}
最佳答案
只是替换
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];
关于ios - 为什么 Notification 适用于 NSHourCalendarUnit 但不适用于 NSWeekCalendarUnit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21230024/
我正在尝试使用这些值设置 UILocalNotification 的重复间隔,但一如既往,Apple 文档非常模糊。 有什么线索吗? 谢谢。 最佳答案 也许看看this blog I just fou
在 iOS 7.0 中,NSWeekCalendarUnit 已被弃用。文档说它必须被替换: Use NSCalendarUnitWeekOfMonth or NSCalendarUnitWeekOf
我不明白为什么 LocalNotification 可以完美地与 NSHourCalendarUnit 一起工作,但永远不会与 NSWeekCalendarUnit 和 NSDayCalendarUn
我是一名优秀的程序员,十分优秀!