SwiftUI中来自日期选择器的用户自定义通知时间

问题描述 投票:0回答:1

因此,到目前为止,我的选择器显示小时和分钟,并且每天下午2点发出通知,我试图将它们绑定在一起,以便用户可以手动选择他们希望通知的时间开始,将时间转换为24小时格式,然后放入->

 dateComponents.hour = 14
 dateComponents.minute = 00

这是我的选择器代码,仅显示时间:

import UserNotifications
import SwiftUI

struct Calendar: View {

    @State private var didTap:Bool = false
    @State var alert = false
    @State private var currentDate = Date()
    @Environment(\.presentationMode) var presentationMode
    @State private var isdateshowing = false
    @State private var textfield = ""

   var body: some View {

    List {



        Section {
             TextField("Choose at when you would like to be reminded", text: $textfield, onEditingChanged: {
                 edit in

                 if edit {
                     self.isdateshowing = true
                 } else {
                     self.isdateshowing = false
                 }
             })
        if isdateshowing {

            DatePicker(selection: .constant(Date()), displayedComponents: .hourAndMinute, label: { Text("Time") })

    }

        }

并且这是一个既请求发送每日通知并启用它的按钮,

        Section {
Button(action: {
    UNUserNotificationCenter.current()
        .requestAuthorization(options: [.alert, .sound, .badge]) { (status, _) in
    if status{

        let content = UNMutableNotificationContent()
        content.title = "It Is Time!"
        content.body = "LETS DO THIS!"
         content.sound = UNNotificationSound.default

        var dateComponents = DateComponents()
        dateComponents.hour = 14
            dateComponents.minute = 00


     //   let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
        let request = UNNotificationRequest(identifier: "noti", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
        return
    }
    self.alert.toggle()

    }
}) {
    Text("Press to Enable Daily Notifications")

    }.alert(isPresented: $alert) {
            return Alert(title: Text("Please Enable Notification Access from Settings Pannel !!"))
                }
            }



        }
    }
}
struct Calendar_Previews: PreviewProvider {
    static var previews: some View {
        Calendar()
    }
}



ios datepicker notifications swiftui timepicker
1个回答
0
投票

尝试这样(未试用):

在您的日历视图中添加:

@State var selectedTime = Date()

和更改:

DatePicker(selection: self.$selectedTime), displayedComponents: .hourAndMinute, label: { Text("Time") })

编辑

[我的建议是,不知道您真正想要实现什么?尝试将您的代码分解为单个元素,例如:

func sendNotification() {
    let content = UNMutableNotificationContent()
    content.title = "It Is Time!"
    content.body = "LETS DO THIS!"
    content.sound = UNNotificationSound.default

    if let thisHour = Calendar.current.dateComponents([.hour], from: Date()).hour,
        let selectedHour = Calendar.current.dateComponents([.hour], from: self.selectedTime).hour {
        // Note, this sends the notification at the selectedHour or later
        if (thisHour >= selectedHour) {
            var dateComponents = DateComponents()
            dateComponents.hour = selectedHour
            dateComponents.minute = 0

            let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
            let request = UNNotificationRequest(identifier: "noti", content: content, trigger: trigger)
            UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
        }
    }
}

然后在您请求许可的部分:

    Section {
        Button(action: {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (status, _) in
                if status {
                    self.sendNotification()
                } else {
                    self.alert.toggle()
                }
            }
        }) {
            Text("Press to Enable Daily Notifications")
        }.alert(isPresented: $alert) {
            Alert(title: Text("Please Enable Notification Access from Settings Pannel !!"))
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.