你好,我正在编写一个超级简单的mac应用程序来从我的日历中检索事件。它应该是非常直截了当的,但不知何故,我没有从EKEventStore获得任何数据,也没有从事件或日历等。
我有以下代码:
func getEvents(completion: @escaping ([EKEvent]) -> ()) {
self.store.requestAccess(to: .event) { granted, error in
guard granted, error == nil else {
print("no access")
return
}
let calendar = Calendar.current
var startComponents = DateComponents()
startComponents.month = -2
let startDate = calendar.date(byAdding: startComponents, to: Date())
var endComponents = DateComponents()
endComponents.day = -1
let endDate = calendar.date(byAdding: endComponents, to: Date())
guard let startDate = startDate, let endDate = endDate else {
print("dates are broken")
completion([])
return
}
let predicate = self.store.predicateForEvents(withStart: startDate, end: endDate, calendars: nil)
print("startDate: \(startDate), endDate: \(endDate), predicate: \(predicate)")
completion(self.store.events(matching: predicate))
}
}让NSCalendarsUsageDescription设置在info.plist中,但是我总是得到一个空数组。我尝试检索所有日历- self.store.calendars(for: .event),以查看我的事件谓词中是否存在错误,并且仍然是一个空数组。
我相信我会做文件上说的每件事。我遗漏了什么吗?
发布于 2022-09-28 07:31:12
根据EKEventStore.calendars is always empty after clean install,尝试在授予权限后立即重新实例化eventStore。
https://stackoverflow.com/questions/70911007
复制相似问题