Local EKCalendar saved with not errors, disappears

2019-09-14 09:26发布

问题:

I'm trying to create a calendar with EKSourceTypeLocal source. Iterate over the self.eventStore.sources, find the one with sourceType == .Local and attempt to create a new calendar with it.

self.calendar = EKCalendar(forEntityType: EKEntityType.Event, eventStore: self.eventStore)
self.calendar?.title = "My Awesome Calendar"
self.calendar?.source = src // src.sourceType == .Local
print("Created \(self.calendar!)")
do {
     try self.eventStore.saveCalendar(self.calendar!, commit: true)
} catch let err as NSError {
      print("Whoops: \(err)")
}

That executes without a problem, and allows me to add some events to that calendar as well. However, when I switch to the native Calendar app, this new one is no there, if I query self.eventStore.calendarsForEntityType(EKEntityType.Event) after the above has completed, it's not there either, and if I restart the app, the calendar I created and all its events are nowhere to be found. What's happening?

回答1:

If you have calendars from other sources active on your device, calendars of type EKSourceTypeLocal will not be visible in the Apple Calendars app.

Additionally, the event store will not show the local calendar when other calendar sources are active.

If you de-activate the other calendars, you should be able to see the saved local calendar in both the Calendars app and the event store.

I’m assuming your app has the necessary permissions obtained using requestAccessToEntityType:completion:.