应用程序特定的本地日历组未保存在 ios 9 但在 ios10 中工作

app specific local calendar groups not saving in ios 9 but working in ios10

`enum CalendarType: String {
    case appointment = "Vyhnes Appointment"
    case event = "Vyhnes Event"
    case shipment = "Vyhnes Shipment"
    static var all = [appointment.rawValue, event.rawValue, 
shipment.rawValue]
}`

func createCalendarGroups(completion: ((_ success: Bool, _ error: NSError?) -> Void)? = nil) {
    let eventStore = EKEventStore()
    eventStore.requestAccess(to: .event, completion: { (granted, error) in
        if (granted) && (error == nil) {
            CalendarType.all.forEach({ (calendarName) in
                if UserDefaults.standard.value(forKey: calendarName) == nil {
                    let newCalendar = EKCalendar(for: .event, eventStore: eventStore)
                    newCalendar.title = calendarName
                    let sourcesInEventStore = eventStore.sources

                    newCalendar.source = sourcesInEventStore.filter{
                        (source: EKSource) -> Bool in
                        source.sourceType.rawValue == EKSourceType.local.rawValue
                        }.first!

                    do {
                        try eventStore.saveCalendar(newCalendar, commit: true)
                        UserDefaults.standard.set(newCalendar.calendarIdentifier, forKey: calendarName)
                    } catch {
                        let alert = UIAlertController(title: "Calendar could not save", message: (error as NSError).localizedDescription, preferredStyle: .alert)
                        let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                        alert.addAction(OKAction)
                        self.present(alert, animated: true, completion: nil)
                    }
                }
            })
            completion?(true, nil)
        } else {
            completion?(false, error as NSError?)
            print(error ?? NSError())
        }
    })
}

//枚举用于三个日历保存三个不同的字符串名称和创建日历。函数 forEach 循环用于迭代枚举 CalendarType 中的 3 个日历名称以保存在具有三个不同组的本地日历中

每个设备 returns 都有自己的 sourceType 。总共有 6 种类型。你可以参考这个link:https://developer.apple.com/documentation/eventkit/eksourcetype

因此您可以通过迭代 sourcetype 数组并将其保存到该类型来检查哪个 sourcetype 可用。