swift - 如何实现 Evenkit 以请求许可
问题描述
我正在构建一个应该在日历中添加提醒的 mac 应用程序。构建没有错误也没有警告,但是当应用程序启动时,我收到以下错误:“提醒失败,错误访问此事件存储是未经授权的。”
我已经在网上搜索请求访问 mac 上日历的正确方法,但没有找到任何方法。
我尝试将以下示例从 ios 翻译为 mac,但失败了:https ://github.com/andrewcbancroft/EventTracker/tree/ask-for-permission
这是我的代码:
import Cocoa
import EventKit
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTableViewDelegate
{
@IBOutlet weak var window: NSWindow!
var eventStore = EKEventStore()
var calendars: [EKCalendar]?
func applicationDidFinishLaunching(_ aNotification: Notification)
{
let reminder = EKReminder(eventStore: self.eventStore)
reminder.title = "Go to the store and buy milk"
reminder.calendar = eventStore.defaultCalendarForNewReminders()
do
{
try eventStore.save(reminder,
commit: true)
} catch let error {
print("Reminder failed with error \(error.localizedDescription)")
}
}
func applicationWillTerminate(_ aNotification: Notification)
{
// Insert code here to tear down your application
}
}
感谢您的关注。
解决方案
例如,您必须调用requestAccess(to:completion:
事件存储
let eventStore = EKEventStore()
eventStore.requestAccess(to: .reminder) { (granted, error) in
if let error = error {
print(error)
return
}
if granted {
// go on managing reminders
}
}
推荐阅读
- c# - Unity Editor 场景为黑色
- reactjs - ant 子菜单无法更改为垂直
- javascript - 网格容器内的卡片:列表中的每个孩子都应该有一个唯一的“key”道具
- bash - 作为兄弟运行 docker
- c++ - C++ 无法打开源文件“time.h”
- macos - 无论如何在MacOS中隐藏菜单栏项目?
- swift - 可选发布者的默认空值扩展?
- c# - 使用带有用于 wpf 的 Visual Studio 代码并连接到 arduino 的 UART 的最佳方式?
- azure - 无法使用电子邮件地址找到具有 upn 的用户:user@domain.com
- jpa - Keycloak MySQL 设置失败,出现错误“等待服务容器稳定性 [300] 秒后超时。操作将回滚。”