繁体   English   中英

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

[英]Access iCloud Calendar Events - macOS App

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

这就是我所做的:

1 - 访问活动商店

1.1我已将权利文件中的 'com.apple.security.personal-information.calendars' 键更改为 YES( 有关此问题的 Stack Overflow 问题)。

项目的.entitlement截图:

图像

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)

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

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

我预计有两个 arrays:

[EKCalendar][EKEvent]

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

谢谢!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM