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!