除1天外,UILocalNotification在特定时间全周开火

时间:2014-07-25 09:46:02

标签: ios objective-c nsdate uilocalnotification

在我的应用中,我有一个LocalNotification。 我从DatePicker获得了开火时间,我希望LocalNotification在特定时间(我从UIPickerView获取)和周六以外的所有工作日开始。

修改

    UIDatePicker *picker = [[UIDatePicker alloc]init];
    [picker setTag:kDatePickerTag];


    for (int i = 0; i < 7; i++) {
        NSDate *today = [NSDate date];

         NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDate *scheduleDate = [**picker.date** dateByAddingTimeInterval:(i * 24.0f * 3600.0f)];
        NSDateComponents *componentsForEachDay = [gregorianCalendar components:NSWeekdayCalendarUnit fromDate:scheduleDate];
        if (componentsForEachDay.weekday != 7) { // To skip Saturday

            UILocalNotification *localNotification = [[UILocalNotification alloc] init];
            localNotification.repeatInterval = NSWeekCalendarUnit;
            localNotification.fireDate = picker.date;
            localNotification.timeZone = [NSTimeZone defaultTimeZone];
            localNotification.alertBody = @"test message";
            localNotification.soundName = UILocalNotificationDefaultSoundName;
            localNotification.applicationIconBadgeNumber = 1;

            [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

我有这个代码到目前为止,但现在我必须设置开火时间,我不知道如何合并我的picker.date给我时间和weedDay给我一天。 我需要两个是相同的NSDate对象。 任何想法?

2 个答案:

答案 0 :(得分:1)

LocalNotifications只有四个InBuilt Intervals,

他们是

case 1:
    notif.repeatInterval = NSMonthCalendarUnit;// monthly
    break;
  case 2:
    notif.repeatInterval = NSYearCalendarUnit;// yearly
    break;
  case 3:
    notif.repeatInterval = NSDayCalendarUnit;//Daily
    break;
  case 4:
    notif.repeatInterval = NSWeekCalendarUnit;// weekly

您无法使用LocalNotifications定义自定义间隔,您可能必须创建自己的逻辑来执行此操作

答案 1 :(得分:1)

如果你想跳过星期六,你必须为其他6天中的每一天设置一个通知,并让它在一周内重复。

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    for (int i = 0; i < 7; i++) {
        NSDate *scheduleDate = [firstScheduledDate dateByAddingTimeInterval:(i * 24.0f * 3600.0f)];
        NSDateComponents *componentsForEachDay = [gregorianCalendar components:NSWeekdayCalendarUnit fromDate:scheduleDate];
        if (componentsForEachDay.weekday != 7) { // To skip Saturday

            UILocalNotification *localNotification = [[UILocalNotification alloc] init];
            localNotification.repeatInterval = NSWeekCalendarUnit;
            localNotification.fireDate = FIRE_DATE;
            localNotification.timeZone = [NSTimeZone defaultTimeZone];
            localNotification.alertBody = YOUR_NOTIFICATION_MESSAGE;
            localNotification.soundName = UILocalNotificationDefaultSoundName;

            [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
        }
    }