swift - 全新安装后 EKEventStore.calendars 始终为空

标签 swift ekeventstore

我的 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/

相关文章:

ios - 从 iOS 中的 UITests 读取 Info.plist?

swift 3 : String search backwards

Swift 全局变量线程安全

ios - 获取用于存储事件的所有可用日历的列表 - iOS (EKEventkit)

swift - swift 泛型函数中的类型可以由返回值决定吗?

ios - 如果用户使用非 iOS 帐户,则以编程方式将新日历添加到日历应用程序

c# - EKEventStore 访问请求在 iOS 10 上崩溃

objective-c - 使用开始日期获取 iPhone 日历中的所有事件

iphone - 由于事件存储更改而导致应用程序崩溃?

ios - 当数据较少时,UITableview 中不会调用scrollViewDidEndDragging