Unable to create local EKCalendar (Reminders) if i

2019-02-18 12:16发布

Hitting a very strange issue here, which seems to me to be an issue with the EventKit API and I just want to check it's nothing I'm doing.

Test case 1:

  • Reminders are enabled in Privacy for the app
  • The device has an iCloud account, but it's set to not sync reminders
  • I can create a local reminders list in the 'Reminders' app from Apple
  • ISSUE - Trying to create a new calendar of entity type EKEntityTypeReminder with a source of type EKSourceTypeLocal fails

Test case 2:

  • Reminders are enabled in Privacy for the app
  • The device has no iCloud account
  • I can create a local reminders list in the 'Reminders' app from Apple
  • I can create a local reminders list via the EK API

Test case 3:

  • Reminders are enabled in Privacy for the app
  • The device has an iCloud account and is set to sync reminders
  • I can create an iCloud reminders list in the 'Reminders' app from Apple
  • I can create an iCloud reminders list via the EK API

Am I going crazy or is this a bug with the API?

Cheers!

Here's the code:

EKCalendar *remindersList = nil;
NSString *remindersListIdent = [[NSUserDefaults standardUserDefaults] objectForKey:kReminderListIdentDefaultsKey];

if(remindersListIdent) {
    remindersList = [store calendarWithIdentifier:remindersListIdent];

    if(remindersList) {
        // has valid reminders list so save reminder and return (don't run rest of function)
        [self saveReminder:reminder toCalendar:remindersList withTypeLabel:reminderTypeLabel];
        return;
    }
}

NSArray *currentCalendars = [store calendarsForEntityType:EKEntityTypeReminder];
for(EKCalendar *cal in currentCalendars) {
    if([[cal title] isEqualToString:@"My App Name"]) {
        remindersList = cal;

        [[NSUserDefaults standardUserDefaults] setObject:[remindersList calendarIdentifier] forKey:kReminderListIdentDefaultsKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [self saveReminder:reminder toCalendar:remindersList withTypeLabel:reminderTypeLabel];
        return;
    }
}

EKSource *localSource = nil;
for (EKSource *source in store.sources) {
    if (source.sourceType == EKSourceTypeCalDAV && [source.title isEqualToString:@"iCloud"]) {
        localSource = source;
        break;
    }
}
if(localSource) {
    remindersList = [self newCalendarListInSource:localSource];
}

if(!remindersList) {
    for (EKSource *source in store.sources) {
        if (source.sourceType == EKSourceTypeLocal) {
            localSource = source;
            remindersList = [self newCalendarListInSource:localSource];
            if(remindersList) {
                break;
            }
        }
    }
}

if(!remindersList) {
    dispatch_async(dispatch_get_main_queue(), ^{
        // show error message
    });
}
else {
    [[NSUserDefaults standardUserDefaults] setObject:[remindersList calendarIdentifier] forKey:kReminderListIdentDefaultsKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self saveReminder:reminder toCalendar:remindersList withTypeLabel:reminderTypeLabel];
}

And this is the contents of newCalendarListInSource:

EKCalendar *remindersList;

remindersList = [EKCalendar calendarForEntityType:EKEntityTypeReminder eventStore:store];
remindersList.source = localSource;
[remindersList setTitle:@"My App Name"];
NSError *listCreateError = nil;
[store saveCalendar:remindersList commit:YES error:&listCreateError];

if(!listCreateError) {
    return remindersList;
}
else {
    NSLog(@"Failed to create reminders list with error: %@", [listCreateError localizedDescription]);

1条回答
够拽才男人
2楼-- · 2019-02-18 13:04

For all still having a problem with this, check your EKSourceType that you use if it exists in your phone. For Swift, try using the EKSourceType.Exchange. Note that I have an Outlook/Exchange account synced to my phone.

I know that this is very vague, but when I used EKSourceType.Exchange, it worked for me. (Play with it)

查看更多
登录 后发表回答