使用Swift 3在OSX Sierra上使用EKEventStore访问日历时出现问题 [英] Problems accessing Calendar using EKEventStore on OSX Sierra with Swift 3
问题描述
这看起来非常简单,但是我已经努力了好几天才能访问OSX上的日历.我已经打开了应用程序沙箱功能,并在应用程序数据中选中了日历"框.我使用以下视图控制器类创建了一个非常简单的应用程序:
This appears very simple, but I've been struggling for several days to get access to the Calendar on OSX. I have switched on the App Sandbox capability, and I've ticked the "Calendar" box in App Data. I have created very simple app with the following view controller class:
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.
As you can see, the only lines of code I've added are to import EventKit and to initialise eventControl.
当我在调试中运行它时,在 eventControl 初始化行出现错误
When I run this in debug, I get an error at the eventControl initialisation line
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";
}
我不知道如何获得正确的权限.
I can't work out how to get the right permissions.
我正在使用Xcode 8.1和macOS Sierra 10.12.1
I am using Xcode 8.1 and macOS Sierra 10.12.1
推荐答案
您需要在info.plist中添加用法说明.这只是为什么需要访问提供给用户的服务的简短说明.
You need to add a usage description to your info.plist. This is a small description of why you need access to the services that is presented to the user.
<key>NSCalendarsUsageDescription</key>
<string>Description of why you need access to the Calendar</string>
这篇关于使用Swift 3在OSX Sierra上使用EKEventStore访问日历时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!