我想在每周三和下午1点重复本地通知消息。
但这段代码的错误是什么?
修改
- (void)applicationDidEnterBackground:(UIApplication *)application {
//Calendar
NSCalendar *gregCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *dateCompnent = [gregCalendar components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekday fromDate:[NSDate date]];
[dateCompnent setHour:18];
[dateCompnent setMinute:03];
[dateCompnent setWeekday:3];
NSDate *date = [gregCalendar dateFromComponents:dateCompnent];
//Notification
UILocalNotification *notification = [[UILocalNotification alloc] init];
[notification setAlertBody:@"Test Notification"];
[notification setFireDate:date];
[notification setSoundName:@"sound.mp3"];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];
}
答案 0 :(得分:0)
仅从UIDatePicker
创建UIDate
来创建NSDateComponents
在这里有点奇怪。
您可以使用dateFromComponents
中的NSCalendar
方法:
NSDate *date = [gregCalendar dateFromComponents:dateCompnent];
另外,请尝试将NSDateComponents
更改为仅使用的组件:
NSCalendar *gregCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *dateCompnent = [gregCalendar components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekday fromDate:[NSDate date]];
[dateCompnent setWeekday:4];
[dateCompnent setHour:13];
[dateCompnent setMinute:00];
修改
不确定哪些是有效的,但我的猜测是重复(因为我没有看到任何与重复UILocalNotification
相关的代码)
查看UILocalNotification
的类引用,更具体地说,查看属性repeatInterval