I tried to create a calendar from my app. It does not work on an iPhone 6, the app crashed.
Here is my code:
do {
let calender = EKCalendar(for: .event, eventStore: eventStore)
calender.title = "MyApp"
calender.source = eventStore.defaultCalendarForNewEvents.source
try eventStore.saveCalendar(calender, commit: true)
userDefaults.set(calender.calendarIdentifier, forKey: calendarIdentifierKey)
userDefaults.synchronize()
} catch {
print("Error occurred while creating calendar ")
}
Update
And the following error is thrown
Error occurred while creating calendar
I finally found the answer.
do {
let calender = EKCalendar(for: .event, eventStore: eventStore)
calender.title = "MyApp"
let sourcesInEventStore = eventStore.sources
let filteredEventStores = sourcesInEventStore.filter{
(source: EKSource) -> Bool in
source.sourceType.rawValue == EKSourceType.local.rawValue || source.title.equalsIgnoreCase("iCloud")
}
if filteredEventStores.count > 0 {
calender.source = filteredEventStores.first!
} else {
calender.source = sourcesInEventStore.filter{
(source: EKSource) -> Bool in
source.sourceType.rawValue == EKSourceType.subscribed.rawValue
}.first!
}
try eventStore.saveCalendar(calender, commit: true)
userDefaults.set(calender.calendarIdentifier, forKey: calendarIdentifierKey)
userDefaults.synchronize()
} catch {
print("Error occurred while creating calendar ")
}