简体   繁体   中英

Access iCloud Calendar Events - macOS App

I want access the users iCloud Calendar events in an app for macOS. While researching I have found some tutorials for iOS but I couldn't find one that works on macOS. I tried to understand Apples Developer Documentation for EventKit but didn't manage to get it running.

Thats what I did:

1 - Accessing the Event Store

1.1 I have changed the 'com.apple.security.personal-information.calendars' key to YES in the entitlements file ( Stack Overflow Question regarding this ).

Screenshot of the.entitlement of the Project:

图像

1.2 Afterwards I tried to request the access (in the 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

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:

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:

[EKCalendar] and [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?

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. 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. This string should be provided with the NSCalendarsUsageDescription key in the info.plist file of your app as described here . 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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