`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())
}
})
}
// Enum用于三个日历,以三个不同的字符串名称和创建日历保存。 function forEach循环用于从枚举CalendarType迭代3个日历名称,以保存在具有三个不同组的本地日历中
答案 0 :(得分:1)
每个设备都返回自己的sourceType。总共有6种类型。您可以参考以下链接:https://developer.apple.com/documentation/eventkit/eksourcetype
因此,您可以通过迭代sourcetype数组来检查哪种sourcetype可用,并将其保存到该类型。