Группы локальных календарей приложения не сохраняются в ios 9, но работают в 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())
}
})
}
//Enum используется для трех календарей для сохранения с тремя разными именами строк и для создания календаря. Функция цикла forEach используется для перебора 3 названий календаря из enum CalendarType для сохранения в локальном календаре с тремя различными группами
1 ответ
Решение
Каждое устройство возвращает свой собственный sourceType . Всего существует 6 типов. Вы можете обратиться по этой ссылке: https://developer.apple.com/documentation/eventkit/eksourcetype
Таким образом, вы можете проверить, какой тип источника доступен, итерируя массив типа источника, и сохранить его для этого типа.