When working with the users calendar, I implemented an EKCalendarChooser which allows the user to select multiple of his calendars. The selected calendar instances are retrieved just fine. Now I later want to use this selection but how can I store it permanently?
My first approach was to use the calendars identifier and store them as a string array to UserDefaults like
@State private var calendarSelection: [EKCalendar]
// my approach to convert the calendar selection into a storable format (string array of ids)
var selectedIds = [String]()
for calendar in calendarSelection {
selectedIds.append(calendar.calendarIdentifier)
}
// now store the string-array, eg. to user defaults:
UserDefaults.standard.set(selectedIds, forKey: "cids")
Unfortunately this doesn't work, because the calendarIdentifier is not a permanent identifier and thus does change over time. As apple states in their documentation:
A full sync with the calendar will lose this identifier. You should have a plan for dealing with a calendar whose identifier is no longer fetch-able by caching its other properties.
How can the user's selection of his calendars be stored then?

Ok, this works for me now.
I store the calendar identifier ...
... then restore it.
This is for 1 selected calendar only (
selectionStyle: .single).Changing this code to multiple calendars should be trivial.
I also tested if the calendar identifier might change ... no joy so far. It remained unchanged during all the tests. If it should change (
nil), no calendar will be selected and the app won't crash.HTH