【问题标题】:Access iCloud Calendar Events - macOS App访问 iCloud 日历事件 - macOS 应用程序
【发布时间】:2019-09-23 16:42:12
【问题描述】:

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

这就是我所做的:

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 开发比较陌生,有人可以帮帮我吗?

谢谢!

【问题讨论】:

    标签: swift macos ekevent ekeventstore


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-17
      • 2017-01-16
      • 2016-01-07
      • 1970-01-01
      • 2022-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多