简体   繁体   English

在 iOS Swift 中设置提醒

[英]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;并且不要在该函数中传递nilcompletion handle the response.处理响应。

尝试以下操作:

EKEntityTypeReminder -> EKEntityType.Reminder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Swift 3中按下创建提醒按钮时设置第二个提醒 - Set a second reminder when create reminder button pressed in Swift 3 在iOS Objective C中使用本地通知设置提醒 - Set Reminder using local notification in iOS Objective C 如何在iOS中选定日期的两周前设置提醒 - How to set a reminder before two weeks of the selected date in ios 基于iOS位置的提醒 - iOS Location based reminder iOS上的每日提醒 - Daily Reminder on iOS iOS 6.0中的提醒API - Reminder API in iOS 6.0 多个提醒警报在iOS v13.0或更高版本中无法使用EventKit快速运行 - Multiple alarms for a reminder is not working in iOS v13.0 or later in swift using eventkit 是否可以在iOS应用程序中设置提醒,以在手机上自定义弹出窗口通知用户而无需运行该应用程序? - Is it possible to set a reminder in an iOS application that notifies the user with a custom popup on the phone without the application having to run? 如何在iOS中为特定时间设置提醒,并在间隔2分钟之后一次又一次(五次)显示提醒 - How to set reminder in iOS for a particular time and show it after 2 min interval again and again(five times) after that time 在iOS应用程序的后台设置提醒 - Setting a reminder in the background of an iOS app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM