我的 iOS 应用程序有一个奇怪的问题,在全新安装后,如果我尝试添加日历事件(在接受 native 日历权限提示后),我的 eventStore 单例永远不会显示任何可用的日历,并且 defaultCalendarForNewEvents
始终为nil
。
我尝试调试它,奇怪的是,如果我尝试 po EKEventStore().calendars(for: .event)
,那么我可以看到它们全部(仍然在全新安装)。
如果我随后终止该应用程序并重新打开它,我的 eventStore 单例会突然开始按预期工作。
我不确定以前是否记录过这种行为,这对我来说很奇怪,而且我找不到涉及此特定问题的其他帖子...
下面是我的 CalendarManager
类的片段以及如何声明 eventStore 单例。
class CalendarManager {
// MARK: Variables
static let sharedInstance = CalendarManager()
fileprivate init() { }
let eventStore = EKEventStore()
// further business logic...
}
再往下看,我遇到问题的代码:
func createEvent(for workout: Workout, sendReminder: Bool = false) -> Bool {
// If I add a breakpoint below and check a new instance of EKEventStore,
// I can see all calendars & defaultCalendarForNewEvents,
// however this `guard` never lets us through...
guard let calendar = CalendarManager.sharedInstance.eventStore.defaultCalendarForNewEvents else { return false }
// create event with default calendar...
}
我做错了什么?
最佳答案
确保您在请求日历权限之前没有初始化 EKEventStore。
关于swift - 全新安装后 EKEventStore.calendars 始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58873603/