Я создаю приложение для 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
}
}
Это сработало. Большое спасибо за такой быстрый и правильный ответ на мой вопрос. Да здравствует переполнение стека!
import Cocoa
import EventKit
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTableViewDelegate {
@IBOutlet weak var window: NSWindow!
var eventStore = EKEventStore()
func applicationDidFinishLaunching(_ aNotification: Notification)
{
eventStore.requestAccess(to: .reminder)
{ (granted, error) in
if let error = error
{
print(error)
return
}
if granted
{
let reminder = EKReminder(eventStore: self.eventStore)
reminder.title = "Go to the store and buy milk"
reminder.calendar = self.eventStore.defaultCalendarForNewReminders()
let date : NSDate = NSDate()
let alarm : EKAlarm = EKAlarm (absoluteDate: date.addingTimeInterval(10) as Date)
reminder.addAlarm(alarm)
do
{
try self.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
}
}
EvenKit? Что это такое?