swift - 如何实现 Evenkit 以请求权限

标签 swift macos

我正在构建一个应在日历中添加提醒的 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
    }
}

关于swift - 如何实现 Evenkit 以请求权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55459423/

相关文章:

iphone - 开始对 OS X 进行逆向工程?

macos - 适用于 Mac OSX 的串行端口模拟器

git - OSX 使用 Beyond Compare 作为 git difftool

ios - 不支持游戏中心成就

ios - 将 TableView 选定的数据分享到whatsapp

在 Mac OS X 中从摄像头捕获视频

iphone - 我怎样才能画出弯曲的阴影?

Swift 从泛型实例化

swift - 消耗品购买的试飞问题 - RevenueCat

ios - 在 UITableViewCell 中设置数据