I am trying to set up daily repeated notifications with swift.
I am able to set up the notification by using
var dateComponents = DateComponents()
dateComponents.hour = 17
dateComponents.minute = 00
But I want the user to be able to change that, I set up a date picker and changed it to time only
In the date picker I was able to get it to save by doing the following
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
loadDate(animation: true)
}
func saveDate() {
UserDefaults.standard.set(TimePicker.date, forKey:dateKey)
}
func loadDate(animation: Bool) {
guard let loadedDate = UserDefaults.standard.object(forKey: dateKey) as? NSDate else { return }
TimePicker.setDate(loadedDate as Date, animated: animation)
}
How can I pass what was selected from hour and minute to the notification time? Also how would I be able to convert the time to 24 hours that way if they select 5 pm it goes to the date component as 17?
Thanks for any help
This is how I have the view controller. inside viewdidload. and then the timepicker is inside another viewcontroller
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound ]){(grandted,error)in }
let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "Body"
var dateComponents = DateComponents()
dateComponents.hour = 17
dateComponents.minute = 18
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let uuidstring = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidstring, content: content, trigger: trigger)
center.add(request) {(error) in}