访问 iCloud 日历事件 - macOS 应用程序
Access iCloud Calendar Events - macOS App
我想在 macOS 应用程序中访问用户的 iCloud 日历事件。在研究过程中,我找到了 iOS 的一些教程,但找不到适用于 macOS 的教程。我试图了解 EventKit 的 Apple 开发者文档,但未能理解 运行ning.
我就是这么做的:
1 - 访问事件存储
1.1 我已将权利文件 (Stack Overflow Question regarding this) 中的 'com.apple.security.personal-information.calendars' 键更改为 YES。
项目.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)
当我 运行 这个应用程序时,我得到以下控制台输出:
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 将使用此字符串向用户解释为什么您的应用程序想要访问用户的日历。如 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.
所述,此字符串应与应用 info.plist
文件中的 NSCalendarsUsageDescription
键一起提供
我想在 macOS 应用程序中访问用户的 iCloud 日历事件。在研究过程中,我找到了 iOS 的一些教程,但找不到适用于 macOS 的教程。我试图了解 EventKit 的 Apple 开发者文档,但未能理解 运行ning.
我就是这么做的:
1 - 访问事件存储
1.1 我已将权利文件 (Stack Overflow Question regarding this) 中的 'com.apple.security.personal-information.calendars' 键更改为 YES。
项目.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)
当我 运行 这个应用程序时,我得到以下控制台输出:
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 将使用此字符串向用户解释为什么您的应用程序想要访问用户的日历。如 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.
info.plist
文件中的 NSCalendarsUsageDescription
键一起提供