使用日期选择器的本地UNMutableNotificationContent触发日期

A.R*_*Roe 1 ios swift swift3 ios10

所以我一直在尝试实现iOS 10 Local Notifications,首先我认为代码与iOS 9相同,只是添加了一个通知扩展目标.然而它实际上使用了这个UNMutableNotificationContent类,

我可以使用时间间隔设置通知,但是如何使用日期选择器专门设置开火日期?

import UserNotifications

let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .full
let displayDate  = dateFormatter.string(from: datePicker.date)

let notif = UNMutableNotificationContent()

notif.title = "Reminder)"
notif.subtitle = "\(displayDate)"
notif.body = "Details"
notif.categoryIdentifier = "Details Reminder"

let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

let request = UNNotificationRequest(identifier: "myNotif", content: notif, trigger: notifTrigger)
Run Code Online (Sandbox Code Playgroud)

UNNotificationRequest触发需要的类型UNTimeIntervalNotificationTrigger,我希望我可以只添加一个日期.

我是否需要使用当前日期和日期选择日期来计算间隔时间?或者我可以通过其他方式做到这一点

Mic*_*ael 5

您需要提供"现在"和希望发送通知的时间间隔.

let interval = laterDate.timeIntervalSince(earlierDate)
Run Code Online (Sandbox Code Playgroud)

在这种情况下,earlierDate应该是'现在'(即只是Date()),我认为laterDate是你的datePicker.date.