[英]Set a reminder in iOS Swift
I am trying to set a simple EKReminder in my swift application to remind users to catch the bus.我试图在我的 swift 应用程序中设置一个简单的 EKReminder 来提醒用户赶上公共汽车。 However, when I try to save my reminder, I always get a error (no error is reported, the app just crashes).但是,当我尝试保存我的提醒时,总是出现错误(没有报告错误,应用程序只是崩溃)。 I have the code below.我有下面的代码。
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)
}
The following should work in Swift 4.2
以下应该适用于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
requires appropriate privacy settings as well. info.plist
需要适当的隐私设置。
I haven't used anything like this before, but looking at your code I can see that you call the requestAccessToEntity
-method, without handling the response.我以前没有使用过这样的东西,但是查看您的代码我可以看到您调用requestAccessToEntity
方法,而不处理响应。 That method will most likely show the user a prompt, asking them to accept that your app has access to "Reminders".该方法很可能会向用户显示提示,要求他们接受您的应用有权访问“提醒”。 With your code, you ask for the permission, but the rest of your code will execute immediately after asking, without 'waiting' for the response.对于您的代码,您请求许可,但其余代码将在请求后立即执行,无需“等待”响应。 The very first time this code runs, the user will be asked, and your reminder will be denied, because it tries to save right away.此代码第一次运行时,将询问用户,并且您的提醒将被拒绝,因为它会立即尝试保存。 Even if your user clicks "allow", your code has already run without permission.即使您的用户单击“允许”,您的代码也已经在未经许可的情况下运行。
Now, if the user clicked allow one time, and then tries to do the same again, then maybe it will work, I don't know.现在,如果用户单击允许一次,然后再次尝试执行相同的操作,那么它可能会起作用,我不知道。 But if your user clicked "Cancel" on the prompt, your code will never work until they go into Settings and allow your app to show reminders.但是,如果您的用户在提示中单击“取消”,则您的代码将永远不会工作,直到他们进入“设置”并允许您的应用显示提醒。
You should not create your reminder before you know if the user allows it, so you should really split this function into two separate functions.您不应该在知道用户是否允许之前创建提醒,因此您应该真正将此功能拆分为两个单独的功能。 And do not pass nil
for completion
in that function;并且不要在该函数中传递nil
来completion
; handle the response.处理响应。
尝试以下操作:
EKEntityTypeReminder -> EKEntityType.Reminder
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.