Mir*_*ira 2 notifications datepicker uikit swift swiftui
我想在用户从 DatePicker 中选择时设置每日通知,但我不知道如何将选定的小时和分钟转换为 DateComponents 以匹配 UNCalendarNotificationTrigger。
这是我的日期选择器:
DatePicker("Choose time of notif", selection: $notifTime, displayedComponents: .hourAndMinute)
Run Code Online (Sandbox Code Playgroud)
这是我的通知触发器:
var dateComponents = DateComponents()
dateComponents = notifTime
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
Run Code Online (Sandbox Code Playgroud)
您只需要使用APIdateComponents从属性中获取,这需要重复。因此,在这种情况下,小时和分钟每天都会重复,因此您可以这样做:dateCalendar
var notifTime: Date
let dateComponents = Calendar.current.dateComponents([.hour, .minute], from: notifTime)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3843 次 |
| 最近记录: |