UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to identify an EKCalendar to store a user calendar selection

Forums > Swift

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?

2      

I wanted to suggest the calendarIdentifier, since that is what I am using in my example EventKit project to persist calendar selection. On the other hand I have the app installed for some time, just checked it and selected calendars are still correctly persisted.

Would be nice if Apple elaborated what full-sync means in this case. As for preserving the selection, maybe you could create hash some sort of hash using other properties and once you cannot find calendars by identifier use this hash? Or maybe this full-sync thing happens rarely (maybe when logs out and in their iCloud) and therefore is not a big deal?

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.