我尝试从我的应用程序创建一个日历。它不适用于iPhone 6,该应用程序崩溃了。
这是我的代码:
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 ")
}
更新
- 在电话中,默认日历设置为 xxx.gmail.com
并抛出以下错误
Error occurred while creating calendar
我终于找到了答案。
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 ")
}