这看起来非常简单,但我已经挣扎了几天才能访问OSX上的日历。我已经打开了App Sandbox功能,并在App Data中勾选了“日历”框。我使用以下视图控制器类创建了非常简单的应用程序:
import Cocoa
import EventKit
class ViewController: NSViewController {
var eventControl = EKEventStore()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
正如您所看到的,我添加的唯一代码行是导入EventKit并初始化eventControl。
当我在调试中运行它时,我在eventControl初始化行中出现错误
2016-10-28 15:02:00.056521 calendarTest[4105:847101] CoreData: XPC: Unable to load metadata: Error Domain=NSCocoaErrorDomain Code=134070 "An error occurred in the persistent store." UserInfo={Problem=request failed, insufficient permission}
2016-10-28 15:02:00.057742 calendarTest[4105:847101] [error] error: -addPersistentStoreWithType:NSXPCStore configuration:(null) URL:file:///Users/patrickramsden/Library/Calendars/Calendar%20Cache options:{
NSInferMappingModelAutomaticallyOption = 1;
NSMigratePersistentStoresAutomaticallyOption = 1;
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";
}
我无法弄清楚如何获得正确的权限。
我正在使用Xcode 8.1和macOS Sierra 10.12.1
答案 0 :(得分:7)
您需要在您的info.plist中添加用法说明。这只是对为什么需要访问提供给用户的服务的简短描述。
<key>NSCalendarsUsageDescription</key>
<string>Description of why you need access to the Calendar</string>
答案 1 :(得分:1)
有没有其他人想到这一点? 我遇到了同样的问题,但尝试访问事件Store中的任何内容都返回nil。即使&#34;访问被授予&#34;。如:
let eventStore = EKEventStore()
switch EKEventStore.authorizationStatus(for: .event) {
case .authorized:
print("Access Granted")
break
case .denied:
print("Access denied")
case .notDetermined:
eventStore.requestAccess(to: .event, completion:
{(granted: Bool, error: Error?) -> Void in
if granted {
print("Access Granted")
} else {
print("Access denied")
}
})
break
default:
print("Case Default")
break
}
print(eventStore.calendars(for: .event))
答案 2 :(得分:0)
获取相同的消息:How to prevent EventStore access error on first run
但是,在用户授予许可后再次启动该应用程序后,访问将正常工作!我的应用未沙盒化。您是否在plist中设置了“隐私-日历使用说明”键?