`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 is used for three calendar to save with three different string names and in create calendar . function forEach loop used to iterate through 3 calendar names from enum CalendarType to save in local calendar with three different groups
Every device returns its own sourceType .Total there are 6 types. You can refer to this link: https://developer.apple.com/documentation/eventkit/eksourcetype
So you can check which sourcetype is available by iterating the array of sourcetype and save it to that type.