使用日期选择器添加通知

Adding notifications using datepicker

导入 UIKit 导入 UserNotifications

class CreateTaskViewController: UIViewController {

@IBOutlet weak var importantSwitch: UISwitch!
@IBOutlet weak var taskNameTextField: UITextField!
@IBOutlet weak var datePicker: UIDatePicker!


    override func viewDidLoad()
    {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    datePicker.addTarget(self, action: #selector(datePickerselectedDate(_:)), for: .valueChanged)
}
@IBAction func datePickerselectedDate(_ sender: Any) {



}
@IBAction func addTapped(_ sender: Any) {


    // Create Task from the outlet info

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    let task = Task(context: context)

    task.name = taskNameTextField.text!
    task.important = importantSwitch.isOn
    (UIApplication.shared.delegate as! AppDelegate).saveContext()

    navigationController!.popViewController(animated: true)


}




    func scheduleNotification(at date: Date) {

        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

        let task = Task(context: context)

        task.name = taskNameTextField.text!
        task.important = importantSwitch.isOn
        (UIApplication.shared.delegate as! AppDelegate).saveContext()


        let calander = Calendar(identifier: .gregorian)
        let components = calander.dateComponents(in: .current, from: date)
        let newComponents = DateComponents(calendar: calander, timeZone: .current,  month: components.month, day: components.day, hour: components.hour, minute: components.minute)

        let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
        let content = UNMutableNotificationContent()
        content.title = "Reminder!!"
        content.body = task.name!
        content.sound = UNNotificationSound.default()

        let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)

        UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
        UNUserNotificationCenter.current().add(request) {(error) in if let error = error {
            print("Uh oh! we had an error:\(error)")

        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge ], completionHandler: {didAllow, error in })




            }

    }


}

}

我没有使用 datePickerselected 函数,因为我不知道将日期添加到我的操作中是什么意思。这就是我现在所拥有的,它在我设置日期后仍然没有通知用户。我希望能够设置一个日期作为通知进行通知。

您已经在 addTapped 操作中创建了 scheduleNotification 函数,因此您无法从其他任何地方调用它。在外面创建它。

@IBAction func addTapped(_ sender: Any) {


// Create Task from the outlet info

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

let task = Task(context: context)

task.name = taskNameTextField.text!
task.important = importantSwitch.isOn
(UIApplication.shared.delegate as! AppDelegate).saveContext()

}

func scheduleNotification(at date: Date) {

    let calander = Calendar(identifier: .gregorian)
    let components = calander.dateComponents(in: .current, from: date)
    let newComponents = DateComponents(calendar: calander, timeZone: .current,  month: components.month, day: components.day, hour: components.hour, minute: components.minute)

    let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
    let content = UNMutableNotificationContent()
    content.title = "Reminder!!"
    content.body = task.name!
    content.sound = UNNotificationSound.default()

    let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)

    UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    UNUserNotificationCenter.current().add(request) {(error) in if let error = error {
        print("Uh oh! we had an error:\(error)")

    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge ], completionHandler: {didAllow, error in })
        }

然后您可以在外部调用它并传递日期选择器操作中的日期。

在您看来确实加载了以下行 -

datePicker.addTarget(self, action: #selector(datePickerselectedDate(_:)), for: .valueChanged)

并且假设您希望您的选择器方法成为您创建的 datePickerselectedDate,请按以下方式修改它

func datePickerselectedDate(_ sender: UIDatePicker) {

    scheduleNotification(date : sender.date)
}

更新 1 更新更改后,只需更改以下方法 -

@IBAction func datePickerselectedDate(_ sender: UIDatePicker) {

       scheduleNotification(date : sender.date)

}