EKAlarm套件未触发警报

时间:2018-10-09 07:30:28

标签: ios swift

我正在制作一个应用中存在警报系统的应用。我在应用程序中编写了以下代码,但不幸的是,该代码无法正常工作。 我正在使用 EKAlarm套件,但无法正常工作。所有权限(如“日历”和“提醒”权限)均已正确授予,但未触发警报。 这是我的代码...

func createReminder() {

    let reminder = EKReminder(eventStore: appDelegate.eventStore!)

    reminder.title = txtField.text!
    let calendar = EKCalendar(for: .reminder, eventStore: 
 appDelegate.eventStore!)

    if appDelegate.eventStore!.sources.count == 0 { // reproducible 
 after Reset Content and Settings
        calendar.source = EKSource()
    }
    else {
        calendar.source = 
 appDelegate.eventStore!.defaultCalendarForNewEvents?.source
    }

    reminder.calendar = calendar
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    let date = datePicker.date


    let alarm = EKAlarm(absoluteDate: dateFormatter.date(from: 
    dateFormatter.string(from: date))!)
    print(dateFormatter.date(from: dateFormatter.string(from: date))!)
    reminder.addAlarm(alarm)
    print(alarm.absoluteDate!)
    do {
        try appDelegate.eventStore?.save(reminder,
                                         commit: true)
    } catch let error {
        print("Reminder failed with error \ . 
   (error.localizedDescription)")
    }
  }
  @IBAction func setAlarm(_ sender: UIButton) {

    if appDelegate.eventStore == nil {
        appDelegate.eventStore = EKEventStore()

        appDelegate.eventStore?.requestAccess(
            to: EKEntityType.reminder, completion: {(granted, error) 
            in
                if !granted {
                    print("Access to store not granted")
                    print(error!.localizedDescription)
                } else {
                    print("Access granted")

                }
        })
    }

    if (appDelegate.eventStore != nil) {
        self.createReminder()
    }

}

请帮助!

0 个答案:

没有答案