我现在已经尝试了太多时间而没有找到问题/解决方案。此代码应该通过比较wd(选定的工作日)和item.itemDayName(当前工作日的Int)来计划从现在起x天的通知。现在,代码仅在wd == 7时有效,如wd == 7 - >选择所有工作日(wd 0-6 = sun-sat)。我将非常感谢您提供的任何反馈,谢谢。
- (void)scheduleNotificationWithItem:(ToDoItem *)item andWeekday:(int)wd{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
if (wd < 7) {
[dateComps setYear:item.itemYear];
[dateComps setMonth:item.itemMonth];
if (item.itemDayName != (wd + 1)) {
int result = (wd + 1) - item.itemDayName;
result = result + item.itemDay;
[dateComps setDay:result];
}
else{
[dateComps setDay:item.itemDay];
}
}
else {
[dateComps setYear:item.itemYear];
[dateComps setMonth:item.itemMonth];
[dateComps setDay:item.itemDay];
}
[dateComps setHour:item.itemHour];
[dateComps setMinute:item.itemMinute];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
//localNotif.fireDate = [itemDate dateByAddingTimeInterval:-(1*60)];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
if (wd < 7) {
[localNotif setRepeatInterval: NSCalendarUnitWeekOfMonth];
}
else {
[localNotif setRepeatInterval: NSCalendarUnitDay]; //Repeat daily
}
localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"Time to take %@!", nil),
item.itemName];
localNotif.alertAction = NSLocalizedString(@"View Details", nil);
localNotif.alertTitle = NSLocalizedString(@"Piller", nil);
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:item.itemName forKey:item.itemKeyName];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}