在EKReminder中设置startDateComponents时出现“无效的日期组件”错误

时间:2012-11-15 07:11:20

标签: iphone ios reminders ekevent

我想保存提醒,但是当我设置StartDateComponents时,它总是说无效日期组件。我尝试了几种方法(你可以看到我的评论代码)来设置值,但是失败了。

EKReminder *reminder = [EKReminder reminderWithEventStore:appDelete.eventStore];

        NSCalendar *gregorian = [NSCalendar currentCalendar];//[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

        reminder.title = @"title";
        NSString *timeStr = @"20121115070800";

        NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
        [inputFormatter setCalendar:gregorian];
        [inputFormatter setDateFormat:@"yyyyMMddHHmmss"];
        [inputFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
        NSDate *parseDate = [inputFormatter dateFromString:timeStr];

        NSDateComponents *dateComponents = [gregorian components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                                       fromDate:parseDate];
        NSDateComponents *timeComponents = [gregorian components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                                       fromDate:parseDate];
        NSDateComponents *dateComps = [[NSDateComponents alloc] init];
        [dateComps setDay:[dateComponents day]];
        [dateComps setMonth:[dateComponents month]];
        [dateComps setYear:[dateComponents year]];
        [dateComps setHour:[timeComponents hour]];
        // Notification will fire in one minute
        [dateComps setMinute:[timeComponents minute]];
        [dateComps setSecond:[timeComponents second]];
        [dateComps setTimeZone:[NSTimeZone localTimeZone]];

        reminder.calendar = /*[NSCalendar autoupdatingCurrentCalendar];*/[eventStore defaultCalendarForNewEvents];
        reminder.calendar = [EKCalendar calendarWithEventStore:eventStore];

        //NSDateComponents *comp = [[NSDateComponents alloc] init];
        /*comp.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        comp.year = [[timeStr substringToIndex:3] integerValue];
        comp.month = [[timeStr substringWithRange:NSMakeRange(4,2)] integerValue];
        comp.day = [[timeStr substringWithRange:NSMakeRange(6, 2)] integerValue];
        comp.hour = [[timeStr substringWithRange:NSMakeRange(8, 2)] integerValue];
        comp.minute = [[timeStr substringWithRange:NSMakeRange(10, 2)] integerValue];
        comp.second = [[timeStr substringWithRange:NSMakeRange(12, 2)] integerValue];*/

        //reminder.startDateComponents = [gregorian components:     (NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit/*|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit*/) fromDate:parseDate];

        [reminder setStartDateComponents:dateComps];// this line will throw the error:Invalid date component

1 个答案:

答案 0 :(得分:1)

似乎主要问题是这两行:

reminder.calendar = /*[NSCalendar autoupdatingCurrentCalendar];*/[eventStore defaultCalendarForNewEvents];
reminder.calendar = [EKCalendar calendarWithEventStore:eventStore];

替换为此行:

reminder.calendar = [eventStore defaultCalendarForNewReminders];   

以下是完整的工作代码,其中还包含一些额外的完整性:

EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityMaskEvent completion:^(BOOL granted, NSError *error) {
    if (granted)
    {
        EKReminder *reminder = [EKReminder reminderWithEventStore:eventStore];

        NSCalendar *gregorian = [NSCalendar currentCalendar];//[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

        reminder.title = @"title44433333";
        NSString *timeStr = @"20121115070800";

        NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
        [inputFormatter setCalendar:gregorian];
        [inputFormatter setDateFormat:@"yyyyMMddHHmmss"];
        [inputFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
        NSDate *parseDate = [inputFormatter dateFromString:timeStr];

        NSDateComponents *dateComponents = [gregorian components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                                        fromDate:parseDate];
        NSDateComponents *timeComponents = [gregorian components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                                        fromDate:parseDate];
        NSDateComponents *dateComps = [[NSDateComponents alloc] init];
        [dateComps setDay:[dateComponents day]];
        [dateComps setMonth:[dateComponents month]];
        [dateComps setYear:[dateComponents year]];
        [dateComps setHour:[timeComponents hour]];
        // Notification will fire in one minute
        [dateComps setMinute:[timeComponents minute]];
        [dateComps setSecond:[timeComponents second]];
        [dateComps setTimeZone:[NSTimeZone localTimeZone]];

        reminder.calendar = [eventStore defaultCalendarForNewReminders];   //here is the replacement

        [reminder setStartDateComponents:dateComps];// this line will throw the error:Invalid date component

        NSError *error = nil;
        [eventStore saveReminder:reminder commit:YES error:&error];
        if (error) NSLog(@"error = %@", error);
    }

}];