特定于应用程序的本地日历组不在ios 9中保存,但在ios 10中工作
//Enum用于三个日历,以三个不同的字符串名称保存,并在“创建日历”中使用。函数forEach loop用于迭代枚举CalendarType中的3个日历名称,并将其保存在本地日历中,其中包含三个不同的组每个设备都返回自己的sourceType。共有6种类型。您可以参考此链接:特定于应用程序的本地日历组不在ios 9中保存,但在ios 10中工作,ios,swift,ekcalendar,Ios,Swift,Ekcalendar,//Enum用于三个日历,以三个不同的字符串名称保存,并在“创建日历”中使用。函数forEach loop用于迭代枚举CalendarType中的3个日历名称,并将其保存在本地日历中,其中包含三个不同的组每个设备都返回自己的sourceType。共有6种类型。您可以参考此链接: 因此,您可以通过迭代sourcetype数组并将其保存为该类型来检查哪个sourcetype可用。谢谢!这对我很有用。我从很多天以来一直面临这个问题。 `enum CalendarType: String { c
因此,您可以通过迭代sourcetype数组并将其保存为该类型来检查哪个sourcetype可用。谢谢!这对我很有用。我从很多天以来一直面临这个问题。
`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())
}
})
}