简体   繁体   English

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

[英]Access iCloud Calendar Events - macOS App

I want access the users iCloud Calendar events in an app for macOS.我想在 macOS 的应用程序中访问用户 iCloud 日历事件。 While researching I have found some tutorials for iOS but I couldn't find one that works on macOS.在研究时,我找到了一些 iOS 的教程,但我找不到适用于 macOS 的教程。 I tried to understand Apples Developer Documentation for EventKit but didn't manage to get it running.我试图了解 EventKit 的 Apple 开发人员文档,但没有设法让它运行。

Thats what I did:这就是我所做的:

1 - Accessing the Event Store 1 - 访问活动商店

1.1 I have changed the 'com.apple.security.personal-information.calendars' key to YES in the entitlements file ( Stack Overflow Question regarding this ). 1.1我已将权利文件中的 'com.apple.security.personal-information.calendars' 键更改为 YES( 有关此问题的 Stack Overflow 问题)。

Screenshot of the.entitlement of the Project:项目的.entitlement截图:

图像

1.2 Afterwards I tried to request the access (in the viewDidLoad) 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 - Getting the Calendar Events 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)

When I run this app I get the following console output:当我运行这个应用程序时,我得到以下控制台 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

I expected two arrays:我预计有两个 arrays:

[EKCalendar] and [EKEvent] [EKCalendar][EKEvent]

I think I really need help here, I have tried a lot but I am relatively new to Swift development, could somebody please help me out?我想我真的需要帮助,我已经尝试了很多,但我对 Swift 开发相对较新,有人可以帮帮我吗?

Thank you!谢谢!

The reason why you are not being able to access the EKEventStore is because you need to provide a description string as to why you want to do that.您无法访问EKEventStore的原因是您需要提供一个描述字符串来说明您为什么要这样做。 This string will be used by the MacOS to provide an explanation to the user why your app wants to have access to the user's calendar. MacOS 将使用此字符串向用户解释为什么您的应用程序想要访问用户的日历。 This string should be provided with the NSCalendarsUsageDescription key in the info.plist file of your app as described here .此字符串应与应用程序的info.plist文件中的NSCalendarsUsageDescription键一起提供,如此所述。 Even though it is often said in the documentation that is needed for iOS applications, that has also been needed for Mac applications since MacOS Mojave as described here .尽管在文档中经常提到 iOS 应用程序所需的文档,但自 MacOS Mojave 起,Mac 应用程序也需要如此所述。

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

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