【发布时间】:2021-01-17 21:41:54
【问题描述】:
在使用用户日历时,我实现了一个 EKCalendarChooser,它允许用户选择他的多个日历。选定的日历实例被检索得很好。现在我稍后想使用此选择,但如何永久存储它?
我的第一种方法是使用日历标识符并将它们作为字符串数组存储到 UserDefaults 之类的
@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")
不幸的是,这不起作用,因为calendarIdentifier 不是永久标识符,因此会随着时间而改变。正如苹果在其文档中所述:
与日历完全同步将丢失此标识符。你应该有一个计划来处理一个日历,它的标识符不再可以通过缓存它的其他属性来获取。
那么如何存储用户对其日历的选择呢?
【问题讨论】:
-
我目前也在为此苦苦挣扎。你喜欢这个解决方案吗?
-
我花了很多时间在这里搜索解决方案,但不幸的是,我最终感到非常惊讶,这似乎是不可能的。如果存在不同的方法,通常就是这种情况,但我也找不到。所以剩下的就是存储一个不太可能改变但仍然可能发生的日历的名称,并且根据用例可能会导致重大问题。
标签: ios swift uniqueidentifier eventkit ekcalendar