使用 EKEventStore 在 OSX Sierra 上使用 Swift 3 访问日历时出现问题
Problems accessing Calendar using EKEventStore on OSX Sierra with Swift 3
这看起来很简单,但我已经努力了好几天才能访问 OSX 上的日历。我已经打开了 App Sandbox 功能,并且在 App Data 中勾选了 "Calendar" 框。我使用以下视图控制器创建了非常简单的应用程序 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。
当我 运行 在调试时,我在 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
还有其他人解决这个问题吗?
我 运行 遇到了同样的问题,但是试图访问事件存储中的任何内容 returns nil。尽管"access is granted"。如:
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))
您需要在 info.plist 中添加使用说明。这是对为什么您需要访问呈现给用户的服务的简短描述。
<key>NSCalendarsUsageDescription</key>
<string>Description of why you need access to the Calendar</string>
收到相同的消息:How to prevent EventStore access error on first run
但是,一旦您在用户授予权限后再次启动应用程序,访问就会正常工作!我的应用程序未被沙盒化。您是否在 plist 中设置了 "Privacy - Calendars Usage Description" 键?
这看起来很简单,但我已经努力了好几天才能访问 OSX 上的日历。我已经打开了 App Sandbox 功能,并且在 App Data 中勾选了 "Calendar" 框。我使用以下视图控制器创建了非常简单的应用程序 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。
当我 运行 在调试时,我在 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
还有其他人解决这个问题吗? 我 运行 遇到了同样的问题,但是试图访问事件存储中的任何内容 returns nil。尽管"access is granted"。如:
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))
您需要在 info.plist 中添加使用说明。这是对为什么您需要访问呈现给用户的服务的简短描述。
<key>NSCalendarsUsageDescription</key>
<string>Description of why you need access to the Calendar</string>
收到相同的消息:How to prevent EventStore access error on first run
但是,一旦您在用户授予权限后再次启动应用程序,访问就会正常工作!我的应用程序未被沙盒化。您是否在 plist 中设置了 "Privacy - Calendars Usage Description" 键?