eventStore.saveCalendar returns an error "json error: An unexpected error occurred."

eventStore.saveCalendar returns an error "json error: An unexpected error occurred."

我试图在 https://www.andrewcbancroft.com/2015/06/17/creating-calendars-with-event-kit-and-swift/ 中实现相同的代码,但我收到一条错误消息,指出 "json error: An unexpected error occurred." 我正在为 macOS 开发此项目。

这是我下面的代码,

@IBAction func addPressed(_ sender: NSButton) {

    // Create an Event Store instance
    let eventStore = EKEventStore();

    // Use Event Store to create a new calendar instance
    // Configure its title
    let newCalendar = EKCalendar(for: .event, eventStore: eventStore)

    // Probably want to prevent someone from saving a calendar
    // if they don't type in a name...
    newCalendar.title = "DCU"

    // Access list of available sources from the Event Store
    let sourcesInEventStore = eventStore.sources

    // Filter the available sources and select the "Local" source to assign to the new calendar's
    // source property
    newCalendar.source = sourcesInEventStore.filter{
        (source: EKSource) -> Bool in
        source.sourceType.rawValue == EKSourceType.local.rawValue
        }.first!

    // Save the calendar using the Event Store instance
    do {
        try eventStore.saveCalendar(newCalendar, commit: true)
    } catch {
        print("json error: \(error.localizedDescription)")
    }
}

当我构建项目并单击 运行 时,出现错误 "json error: An unexpected error occurred."。当我尝试将事件添加到 defaultcalendar 时,它给我一个错误 "json error: No calendar was set"。我已经尝试了所有可用的在线资源,但找不到解决方案。不知何故,eventStore.save 或 eventStore.saveCalendar 都不适合我。有人可以让我知道我哪里出错了吗?

两个可能的原因

  1. 来自EKEventStore

    的文档

    To access the user’s Calendar data, all sandboxed macOS apps must include the com.apple.security.personal-information.calendars entitlement. To learn more about entitlements related to App Sandbox, see Enabling App Sandbox.

  2. 如有必要请勾选 authorizationStatus of the store and call requestAccess


您的错误信息具有误导性,错误与JSON无关。