-1

您好我正在尝试实施日期/时间通知,但日期是 2 小时。

选择日期时的代码

func SelectTime(sender: UIView){

    formatter.timeZone = TimeZone.autoupdatingCurrent
    let timePicker = ActionSheetDatePicker(title: "Time:", datePickerMode: UIDatePickerMode.time, selectedDate: userDate, doneBlock: {
        picker, userDateWithTime, index in

        self.formatter.timeZone = TimeZone.autoupdatingCurrent

        self.formatter.dateFormat = "yyyy MMMM dd, HH:mm"
        self.dateSelected.text = self.formatter.string(for: userDateWithTime)

        //print("User date picked \(self.formatter.string(from: userDateWithTime as! Date))")
        return

创建通知的代码

let uuid = UUID().uuidString
    let notification = UNMutableNotificationContent()
    notification.title = "Plus - Todo"
    //notification.subtitle
    notification.body = taskText.text!

    let calendar = Calendar.current
    let dateComponents = calendar.dateComponents(in: .current, from: userDateWithTime)

    //let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: correctDate!)


    let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
    //let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    let request = UNNotificationRequest(identifier: uuid, content: notification, trigger: notificationTrigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
4

1 回答 1

0

问题是 userPickedDateWithTime 的类型是 Any 而不是 Date。我通过创建一个带有日期类型的 formattedDate 并将 formattedDate = userPickedDateWithTime 设置为解决了这个问题!日期,然后在创建通知时使用 formattedDate 而不是 userPickedDateWithTime

var formattedDate = Date()
func SelectTime(sender: UIView){

formatter.timeZone = TimeZone.autoupdatingCurrent
let timePicker = ActionSheetDatePicker(title: "Time:", datePickerMode: UIDatePickerMode.time, selectedDate: userDate, doneBlock: {
    picker, userDateWithTime, index in

    self.formatter.timeZone = TimeZone.autoupdatingCurrent

    self.formatter.dateFormat = "yyyy MMMM dd, HH:mm"
    self.dateSelected.text = self.formatter.string(for: userDateWithTime)

    //print("User date picked \(self.formatter.string(from: userDateWithTime as! Date))")
    formattedDate = userPickedDateWithtime as! Date // <-- This solved the problem

    return

通知代码

let uuid = UUID().uuidString
let notification = UNMutableNotificationContent()
notification.title = "Plus - Todo"
//notification.subtitle
notification.body = taskText.text!

let calendar = Calendar.current
let dateComponents = calendar.dateComponents(in: .current, from: formattedDate)

//let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: correctDate!)


let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
//let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: uuid, content: notification, trigger: notificationTrigger)

UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
于 2017-07-30T14:10:13.180 回答