4

我正在尝试从我的应用程序中保存/检索提醒,但由于某种原因,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);
    }
}];

而且我的新提醒永远不会出现!

谁能告诉我我在这里做错了什么?

4

3 回答 3

2

-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];
    }
}
于 2013-03-06T07:41:04.587 回答
0

I got this to work by switching up the localSource to be EKSourceTypeCalDAV. Not totally sure why that was required, maybe someone can shed some light on that?

于 2013-03-07T00:00:34.457 回答
0

希望这对你有用。

    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);
    }

如果您在将事件保存到设备日历时遇到问题,请查看我的这篇文章。

于 2013-04-25T11:39:53.470 回答