Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 安排本地通知_Ios_Objective C_Uilocalnotification_Repeat - Fatal编程技术网

Ios 安排本地通知

Ios 安排本地通知,ios,objective-c,uilocalnotification,repeat,Ios,Objective C,Uilocalnotification,Repeat,我正在开发一个功能,允许用户安排接收通知的天数和时间 到目前为止,“时间和信息”功能运行良好。我被卡住的地方是重复间隔 以下是我尝试过的: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; NSString *formatedDate = [dateForm

我正在开发一个功能,允许用户安排接收通知的天数和时间

到目前为止,“时间和信息”功能运行良好。我被卡住的地方是重复间隔

以下是我尝试过的:

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setTimeStyle:NSDateFormatterShortStyle];

        NSString *formatedDate = [dateFormatter stringFromDate:self.datePicker.date];
        self.currentTimeLabel.text = formatedDate;

        NSDate *date = [dateFormatter dateFromString:self.currentTimeLabel.text];

        NSCalendar *calendar = [NSCalendar currentCalendar];

        NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];
        NSInteger hour = [components hour];
        NSInteger minute = [components minute];

        [components setHour:hour];
        [components setMinute:minute];

        if ([self.currentRepeatLabel.text containsString:@"Sun"]) {
            [components setWeekday:0];

            self.notification = [[UILocalNotification alloc] init];
            self.notification.fireDate = [calendar dateFromComponents:components];
            self.notification.repeatInterval = NSCalendarUnitDay;
            [self.notification setAlertBody:[NSString stringWithFormat:@"A friendly reminder: %@", self.titleStringToDisplay]];

            if (self.soundSwitch.isOn == YES) {
                self.notification.soundName = @"soundeffect.wav";
            }

            NSDictionary *infoDict = [NSDictionary dictionaryWithObject:self.titleStringToDisplay forKey:@"title"];
            self.notification.userInfo = infoDict;

            [[UIApplication sharedApplication] scheduleLocalNotification:self.notification];
        }
        if ([self.currentRepeatLabel.text containsString:@"Mon"]) {
            [components setWeekday:1];

            self.notification = [[UILocalNotification alloc] init];
            self.notification.fireDate = [calendar dateFromComponents:components];
            self.notification.repeatInterval = NSCalendarUnitDay;
            [self.notification setAlertBody:[NSString stringWithFormat:@"A friendly reminder: %@", self.titleStringToDisplay]];

            if (self.soundSwitch.isOn == YES) {
                self.notification.soundName = @"soundeffect.wav";
            }

            NSDictionary *infoDict = [NSDictionary dictionaryWithObject:self.titleStringToDisplay forKey:@"title"];
            self.notification.userInfo = infoDict;

            [[UIApplication sharedApplication] scheduleLocalNotification:self.notification];
        }
// I'm doing this for each day.
NSCalendarUnitDay
每天都在重复我的通知,不管我选择了哪天。我已经尝试了
NSCalendarUnitWeekOfYear
,但使用它时,我的通知从未发出

用户的目标是设置标题、时间和重复天数(与本机alarm应用程序非常相似)

如何设置此操作的重复间隔

更新和新问题:

我现在正在使用
NSCalendarUnitWeekOfYear


这是我的问题。。。通知现在不再触发,repeatInterval总是设置为星期一,而不是代码中的一周中的某一天。

有几种方法可以做到这一点。您可以使用NScalender查找工作日,然后计算您希望在其中工作的日期

-(void) calculateweeknumber
{

    NSCalendar *numberCalendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [numberCalendar components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
    [comps setSecond:0.0];
    weekday = [comps weekday];
    NSLog(@"number is %d",weekday);
}

有几种方法可以做到这一点。您可以使用NScalender查找工作日,然后计算您希望在其中工作的日期

-(void) calculateweeknumber
{

    NSCalendar *numberCalendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [numberCalendar components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
    [comps setSecond:0.0];
    weekday = [comps weekday];
    NSLog(@"number is %d",weekday);
}

你的代码有很多问题

显然,你的约会对象来自一个
UIDatePicker
。你应该用这个日期。来回转换它是胡说八道

此外,还要清除所有重复的代码。这使得理解(和修复)代码更加困难。它隐藏了逻辑

但真正的问题是,您只是将日期组件设置为一个日期。事实并非如此。您首先在组件中输入一些日期,然后设置一个工作日。这不会给出另一个有效日期-为什么要这样做?怎么可能呢?它应该调整什么?那一天?这个月?那一年?进入未来还是进入过去?转换为日期时,工作日似乎被忽略


您需要计算一个正确的日期(描述如何计算下周一)。

您的代码有很多问题

显然,你的约会对象来自一个
UIDatePicker
。你应该用这个日期。来回转换它是胡说八道

此外,还要清除所有重复的代码。这使得理解(和修复)代码更加困难。它隐藏了逻辑

但真正的问题是,您只是将日期组件设置为一个日期。事实并非如此。您首先在组件中输入一些日期,然后设置一个工作日。这不会给出另一个有效日期-为什么要这样做?怎么可能呢?它应该调整什么?那一天?这个月?那一年?进入未来还是进入过去?转换为日期时,工作日似乎被忽略


您需要计算一个正确的日期(说明如何计算下周一)。

在这种情况下,什么是工作日?工作日是日历的当前日期,就像今天是星期五,所以星期五的工作日是6@LukeIrvinGotcha。好的,这是我对上面代码的问题。我所有的重复都是在每周一进行的,即使当我一步一步地执行代码时,它是在一周中我设置的日期中进行的。你称这行为CanceloCalNotification??在这个场景中,什么是工作日?工作日是日历的当前日期,就像今天是星期五,所以星期五的工作日是6@LukeIrvinGotcha。好的,这是我对上面代码的问题。我所有的重复都是在每周一进行的,即使当我一步一步地完成代码时,它也在一周中的几天内完成,我将其设置为。你称这行为cancelLocalNotification??