【发布时间】:2015-03-02 23:30:46
【问题描述】:
我正在构建一个应用程序,允许用户在每个工作日重复本地通知,因为苹果没有 WeekDays 的重复间隔,我需要为每个工作日创建本地通知。我做了一个 for 循环循环 5 次工作周中的天数。然而,它似乎并没有被解雇。但是,当我要删除所有五个通知时,就不会触发。这就是我的代码的样子。
NSDate *today = [NSDate date];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = _alarmTime;
localNotification.alertBody = @"Wake up or pay-up!";
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.alertAction = @"Wake up or pay-up!";
localNotification.soundName = @"sound_ring.caf";
NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:_alarmTime];
switch (_repeatInt) {
case 1:
localNotification.repeatInterval = NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
_repeatString = @"Everyday";
break;
case 2:
//Attempting to create 5 local notifications with different days but same time.
for (int i = 2; i <= 6; i++){
[dateComponent setWeekday:i]; // 2 = mon // 3= tues // 4 = wends // 5 = thurs // 6 = fri
NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];
UILocalNotification *localNotification2 = [localNotification copy];
localNotification2.fireDate = fireDate;
localNotification2.repeatInterval = NSWeekCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
NSLog(@"%D",i);
}
如果有人有任何想法,请告诉我!提前致谢。
更新:
NSDate *startDate = [gregCalendar dateFromComponents:dateComponent];
[dateComponent setMonth:0];
[dateComponent setDay:i]; // 2 = mon // 3= tues // 4 = wends // 5 = thurs // 6 = fri
[dateComponent setYear:0];
NSDate *fireDate = [gregCalendar dateByAddingComponents:dateComponent toDate:startDate options:0];
UILocalNotification *localNotification2 = [localNotification copy];
localNotification2.fireDate = fireDate;
localNotification2.repeatInterval = NSCalendarUnitWeekOfYear;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
NSLog(@"%D",i);
【问题讨论】:
-
但是...您在 AppDelegate 中有...注册发送通知的代码吗?
-
@TonyMkenu 是的,我已经有了。
-
@jack 在您的代码中声明“_repeatInt”的位置。请让我知道它需要。
标签: ios ios8 nsdate uilocalnotification repeat