我正在尝试使用Eventkit来访问Mac日历。虽然我在应用程序中有本地日历,iCloud日历和Google日历中的多个日历和许多事件,但已成功请求访问但我仍然获得零或一系列日历或事件。 我从以下代码得到的输出是:在我的Mac [] []
let sources = eventStore.sources
for source in sources{
print(source.title)
for calendar in source.calendars(for: .event){
print(calendar.title)
}
}
let calendars = eventStore.calendars(for: .event)
let predicate = self.eventStore.predicateForEvents(withStart: startDate, end: endDate, calendars: nil)
let events = self.eventStore.events(matching: predicate)
print(calendars)
print(events)
如果我尝试从代码创建并保存日历,那么我收到错误:错误Domain = EKErrorDomain Code = 5“当持久性不可用时尝试保存”UserInfo = {NSLocalizedDescription =在持久性不可用时尝试保存}
经过大量的反复试验,我找到了答案。
您需要在权利文件中将com.apple.security.personal-information.calendars
密钥设置为YES,即使您的应用程序未进行沙盒处理也是如此。 Apple的EventKit实施中存在一个错误,如果未设置此密钥,即使沙箱被禁用,也会阻止您的应用访问日历。
我可能错了,但是你在谓词中缺少一个参数,你传递的是nil,所以基本上没有任何输出排序。尝试将代码更改为:
let sources = eventStore.sources
for source in sources{
print(source.title)
for calendar in source.calendars(for: .event){
print(calendar.title)
}
}
let calendars = eventStore.calendars(for: .event)
let predicate = self.eventStore.predicateForEvents(withStart: startDate, end: endDate, calendars: calendars) //change here
let events = self.eventStore.events(matching: predicate)
print(calendars)
print(events)