我正在设置一些本地通知作为提醒。到目前为止,我已经能够设置一个非重复通知,该通知从从 datePicker 选择的日期触发。
let dateformatter = DateFormatter()
dateformatter.dateStyle = DateFormatter.Style.medium
dateformatter.timeStyle = DateFormatter.Style.short
let dateFromString = dateformatter.date(from: selectDateTextField.text!)
let fireDateOfNotification: Date = dateFromString!
//if permission allowed
let content = UNMutableNotificationContent()
content.title = notifTitleTextField.text!
content.body = notifNoteTextView.text
content.sound = UNNotificationSound.default()
let triggerDate = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute,.second,], from: fireDateOfNotification)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate,
repeats: false)
//Schedule the Notification
let titleNospace = notifTitleTextField.text?.replacingOccurrences(of: " ", with: "")
let identifier = titleNospace
let request = UNNotificationRequest(identifier: identifier!, content: content, trigger: trigger)
self.center.add(request, withCompletionHandler: { (error) in
if let error = error {
print(error.localizedDescription)
}
})
现在我希望用户从列表(或选择器)中选择重复间隔(每小时、每天、每周、每月、每年或每 x 天)。有没有一种简单的方法可以做到这一点,或者我需要创建一个自定义类?通过一系列if else语句来实现它是否正确?(在我看来有点不对劲,似乎不是正确的方法)谢谢。