eventStore.saveCalendar返回错误“json error:发生意外错误”。

问题描述 投票:0回答:1

我试图在https://www.andrewcbancroft.com/2015/06/17/creating-calendars-with-event-kit-and-swift/中实现相同的代码,但我收到一条错误,指出“json错误:发生意外错误。”我正在为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:发生意外错误。”。当我尝试向defaultcalendar添加一个事件时,它给我一个错误“json错误:没有设置日历”。我已经在线尝试了所有可用的资源,但找不到解决方案。不知何故,eventStore.save或eventStore.saveCalendar都不适用于我。有人可以告诉我哪里可能出错?

swift eventkit
1个回答
0
投票

两个可能的原因

  1. 来自EKEventStore的文档 要访问用户的日历数据,所有沙盒macOS应用程序必须包含com.apple.security.personal-information.calendars权利。要了解有关App Sandbox相关权利的更多信息,请参阅Enabling App Sandbox
  2. 检查商店的authorizationStatus,如有必要,请致电requestAccess

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

© www.soinside.com 2019 - 2024. All rights reserved.