Failing to save EKReminder in iOS 6

2020-07-18 08:09发布

I am trying to save/ retrieve reminders from my app, but for some reason the EKReminder seems as though it is not being saved. Here is my code:

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

}];

Then later on, I am trying to verify that the reminder has in fact been saved. I do that by using this code:

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

And my new reminder never shows up!

Can anyone tell me what I am doing wrong here??

3条回答
劳资没心,怎么记你
2楼-- · 2020-07-18 08:19

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?

查看更多
霸刀☆藐视天下
3楼-- · 2020-07-18 08:21

Hope this works for you.

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

And check my this post if you are having trouble in saving an event to device calendar.

查看更多
Emotional °昔
4楼-- · 2020-07-18 08:26

You only need to call -requestAccessToEntityType:completion: if the authorization status is EKAuthorizationStatusNotDetermined. You can check the authorization status using +[EKEventStore authorizationStatusForEntityType:].

The completion block will then only be called the first time you call -requestAccessToEntityType:completion. On the Simulator it won't even be called once, because on the Simulator your app will already have access.

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];
    }
}
查看更多
登录 后发表回答