未按规定保存EKReminder在iOS 6中(Failing to save EKReminder

2019-08-17 08:14发布

我想保存/从我的应用程序获取提醒,但由于某些原因,EKReminder好像它不被保存。 这里是我的代码:

EKEventStore * _eventStore = [[EKEventStore alloc] init];
[_eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {

    //create a new calendar for reminders.
    EKCalendar *calendar = [EKCalendar calendarForEntityType:EKEntityTypeReminder eventStore:_eventStore];
    EKSource *localSource = nil;
    for (EKSource *source in _eventStore.sources)
        if (source.sourceType == EKSourceTypeLocal)
        {
            localSource = source;
            break;
        }

    calendar.source = localSource;

    self.calendarIdentifier = calendar.calendarIdentifier;
    EKReminder *reminder = [EKReminder reminderWithEventStore:_eventStore];
    reminder.calendar = calendar;
    reminder.title = @"Test Reminder";
    reminder.startDateComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
    reminder.completionDate = [NSDate dateWithTimeIntervalSinceNow:100];

    EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:[NSDate dateWithTimeIntervalSinceNow:100]];
    reminder.alarms = @[alarm];      

    NSError *errorOb = nil;
    BOOL saved = [_eventStore saveReminder:reminder commit:YES error:&errorOb];
    if (saved) NSLog(@"Saved Reminder");
    else NSLog(@"Failed to save reminder");

}];

再后来,我试图验证提醒已在事实上被保存。 我这样做,通过使用此代码:

NSPredicate *predicate = [_eventStore predicateForRemindersInCalendars:nil];
[_eventStore fetchRemindersMatchingPredicate:predicate completion:^(NSArray *reminders) {
    for (EKReminder *reminder in reminders) {
        NSLog(@"Reminder Title: %@", reminder.title);
    }
}];

而我的新的提醒一直没有出现!

谁能告诉我什么,我做错了什么?

Answer 1:

你只需要调用-requestAccessToEntityType:completion:如果授权状态EKAuthorizationStatusNotDetermined 。 您可以检查使用授权状态+[EKEventStore authorizationStatusForEntityType:]

建成块将仅被称为第一次调用-requestAccessToEntityType:completion 。 在模拟器上它甚至不会被调用一次,因为在你的应用程序将已经访问模拟器。

EKEventStore * _eventStore = [[EKEventStore alloc] init];
EKAuthorizationStatus authorizationStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder];
if (authorizationStatus == EKAuthorizationStatusNotDetermined) {
    [_eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {
        if (granted) {
            [self createNewReminder];
        } else {
            [self showNowAccessAlert];
        }
    }];
} else {
    if (authorizationStatus == EKAuthorizationStatusAuthorized) {
        [self createNewReminder];
    } else {
        [self showNowAccessAlert];
    }
}


Answer 2:

我得到这个切换了localSource是EKSourceTypeCalDAV工作。 不能完全确定为什么这是必需的,也许有人可以阐明一些轻?



Answer 3:

希望这对你的作品。

    if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
    {
        // iOS 6 and later
        // asks user to allow application to use his device calendar
        [eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error)
        {
            if (granted)
            {
                EKReminder *reminder = [EKReminder reminderWithEventStore:eventStore];
                reminder.title = aNotification;
                reminder.calendar = [eventStore defaultCalendarForNewReminders];
                NSDate *date = //aDate;
                EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date];
                [reminder addAlarm:alarm];
                NSError *error = nil;
                [eventStore saveReminder:reminder commit:YES error:&error];

                if(error)
                    NSLog(@"unable to Reminder!: Error= %@", error);
            }
        }];
    }
    // iOS < 6
    else
    {
        EKReminder *reminder = [EKReminder reminderWithEventStore:eventStore];
        reminder.title = aNotification;
        reminder.calendar = [eventStore defaultCalendarForNewReminders];
        NSDate *date = //aDate;
        EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date];
        [reminder addAlarm:alarm];
        NSError *error = nil;
        [eventStore saveReminder:reminder commit:YES error:&error];

        if(error)
            NSLog(@"unable to Reminder!: Error= %@", error);
    }

并检查我的这篇文章 ,如果你是在拯救一个事件到设备的日历有麻烦。



文章来源: Failing to save EKReminder in iOS 6