gpt4 book ai didi

ios - 在 iOS Swift 中设置提醒

转载 作者:可可西里 更新时间:2023-11-01 02:20:07 25 4
gpt4 key购买 nike

我正在尝试在我的 swift 应用程序中设置一个简单的 EKReminder 来提醒用户 catch 公共(public)汽车。但是,当我尝试保存我的提醒时,我总是会收到错误消息(没有错误报告,应用程序只是崩溃了)。我有下面的代码。

public class func createReminder(reminderTitle: String, timeInterval:      NSDate) {
var calendarDatabase = EKEventStore()

calendarDatabase.requestAccessToEntityType(EKEntityTypeReminder,
completion: nil)

let reminder = EKReminder(eventStore: calendarDatabase)

reminder.title = reminderTitle

let alarm = EKAlarm(absoluteDate: timeInterval)

reminder.addAlarm(alarm)

reminder.calendar = calendarDatabase.defaultCalendarForNewReminders()

var error: NSError?

calendarDatabase.saveReminder(reminder, commit: true, error: &error)
}

最佳答案

以下应该在 Swift 4.2 中工作

func AddReminder() {

eventStore.requestAccess(to: EKEntityType.reminder, completion: {
granted, error in
if (granted) && (error == nil) {
print("granted \(granted)")


let reminder:EKReminder = EKReminder(eventStore: self.eventStore)
reminder.title = "Must do this!"
reminder.priority = 2

// How to show completed
//reminder.completionDate = Date()

reminder.notes = "...this is a note"


let alarmTime = Date().addingTimeInterval(1*60*24*3)
let alarm = EKAlarm(absoluteDate: alarmTime)
reminder.addAlarm(alarm)

reminder.calendar = self.eventStore.defaultCalendarForNewReminders()


do {
try self.eventStore.save(reminder, commit: true)
} catch {
print("Cannot save")
return
}
print("Reminder saved")
}
})

}

info.plist 也需要适当的隐私设置。 enter image description here

关于ios - 在 iOS Swift 中设置提醒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31235356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com