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

标签 swift macos ekevent ekeventstore

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

这就是我所做的:

1 - 访问事件商店

1.1 我已将权利文件 (Stack Overflow Question regarding this) 中的“com.apple.security.personal-information.calendars”键更改为 YES。

项目.entitlement截图:

img

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 将使用此字符串向用户解释为什么您的应用程序想要访问用户的日历。此字符串应与应用的 info.plist 文件中的 NSCalendarsUsageDescription 键一起提供,如 here 所述。 .尽管在文档中经常说 iOS 应用程序需要,但自 MacOS Mojave 以来 Mac 应用程序也需要它,如所述here .

关于swift - 访问 iCloud 日历事件 - macOS 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58066794/

相关文章:

iOS9 联系人框架从新保存的联系人中获取标识符

ios - 自定义 UISegmentedControl : how change tint color of selected segment's image?

macos - 如何使用 gluOrtho2D(像素空间)在 Mac OSX(Snow Leopard)上启动 OpenGL 上下文

objective-c - 如何获取 SecKeychainRef 的属性?

ios - 有没有办法创建每小时的 RecurrenceRule?

ios - 如何在 iphone 日历中跟踪我的应用程序添加的事件

swift - 在 Swift 中单击右侧菜单时左侧菜单打开

ios - 为什么使用 JSONEncoder 编码字符串会添加反斜杠?

objective-c - 打开多个文件的大中央策略

ios - EKEventEditViewController 打开速度非常慢