gpt4 book ai didi

swift - 访问 iCloud 日历事件 - macOS 应用程序

转载 作者:行者123 更新时间:2023-11-28 07:21:01 25 4
gpt4 key购买 nike

我想在 macOS 应用程序中访问用户的 iCloud 日历事件。在研究过程中,我找到了一些适用于 iOS 的教程,但找不到适用于 macOS 的教程。我试图了解 EventKit 的 Apple 开发者文档,但没能让它运行起来。

这就是我所做的:

1 - 访问事件商店

1.1 我已将权利文件 (Stack Overflow Question regarding this) 中的“com.apple.security.personal-information.calendars”键更改为 YES。

项目.entitlement截图:

img

1.2 之后我尝试请求访问(在 viewDidLoad 中)

let eventStore = EKEventStore()

switch EKEventStore.authorizationStatus(for: .event) {
case .authorized:
print("Acess granted")

case .denied:
print("Access denied")

case .notDetermined:
eventStore.requestAccess(to: .event, completion: {
(granted, error) in

if granted {
print("granted \(granted)")

}else {
print("error \(String(describing: error))")
}

})
default:
print("Case default")
}

2 - 获取日历事件

let sources = eventStore.sources
for source in sources{
print(source.title)
for calendar in source.calendars(for: .event){
print(calendar.title)
}
}

// create dates
let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd HH:mm"

let startDate = formatter.date(from: "2019/9/12 0:01")!
let endDate = formatter.date(from: "2019/9/12 23:59")!


let calendars = eventStore.calendars(for: .event)
let predicate = eventStore.predicateForEvents(withStart: startDate, end: endDate, calendars: calendars)
let events = eventStore.events(matching: predicate)
print(calendars)
print(events)

当我运行这个应用程序时,我得到以下控制台输出:

getCalendarEvents[1970:100712] CoreData: XPC: Unable to load metadata: Error Domain=NSCocoaErrorDomain Code=134070 "An error occurred in the persistent store." UserInfo={Problem=request failed, insufficient permission}
2019-09-23 18:35:24.981947+0200 getCalendarEvents[1970:100712] [error] error: -addPersistentStoreWithType:NSXPCStore configuration:(null) URL:file:///Users/henri/Library/Calendars/Calendar%20Cache options:{
NSInferMappingModelAutomaticallyOption = 1;
NSMigratePersistentStoresAutomaticallyOption = 1;
NSPersistentHistoryTrackingKey = {
NSPersistentHistoryTrackingEntitiesToExclude = (
ChangeRequest
);
};
agentOrDaemon = 1;
serviceName = "com.apple.CalendarAgent.database";
} ... returned error Error Domain=NSCocoaErrorDomain Code=134070 "An error occurred in the persistent store." UserInfo={Problem=request failed, insufficient permission} with userInfo dictionary {
Problem = "request failed, insufficient permission";
}
CoreData: error: -addPersistentStoreWithType:NSXPCStore configuration:(null) URL:file:///Users/henri/Library/Calendars/Calendar%20Cache options:{
NSInferMappingModelAutomaticallyOption = 1;
NSMigratePersistentStoresAutomaticallyOption = 1;
NSPersistentHistoryTrackingKey = {
NSPersistentHistoryTrackingEntitiesToExclude = (
ChangeRequest
);
};
agentOrDaemon = 1;
serviceName = "com.apple.CalendarAgent.database";
} ... returned error Error Domain=NSCocoaErrorDomain Code=134070 "An error occurred in the persistent store." UserInfo={Problem=request failed, insufficient permission} with userInfo dictionary {
Problem = "request failed, insufficient permission";
}
[]
[]
error nil

我期望有两个数组:

[EKCalendar][EKEvent]

我想我真的需要帮助,我已经尝试了很多,但我对 Swift 开发还比较陌生,有人可以帮我吗?

谢谢!

最佳答案

您无法访问 EKEventStore 的原因是您需要提供一个描述字符串来说明您为什么要这样做。 MacOS 将使用此字符串向用户解释为什么您的应用程序想要访问用户的日历。此字符串应与应用的 info.plist 文件中的 NSCalendarsUsageDescription 键一起提供,如 here 所述。 .尽管在文档中经常说 iOS 应用程序需要,但自 MacOS Mojave 以来 Mac 应用程序也需要它,如所述here .

关于swift - 访问 iCloud 日历事件 - macOS 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58066794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com