如何在 Swift 3 中创建特定于应用程序的日历



我尝试从我的应用程序创建一个日历。它不适用于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 ")
}

更新

  1. 在电话中,默认日历设置为 xxx.gmail.com
  2. 并抛出以下错误

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

相关内容

  • 没有找到相关文章

最新更新