我正在尝试编写一些代码,允许用户设置自定义本地通知以提醒他们一些任务 . 我正在使用用户通知框架 . 因此,基本上用户可以决定设置通知,提醒他每周购买一些肥皂或每年生日祝福或每个月检查一些东西(这些只是示例) .
到目前为止,我已经能够设置一个没有重复间隔的本地通知 .
func scheduleNotif() {
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 notif are allowed
let content = UNMutableNotificationContent()
content.title = notifTitleTextField.text!
content.body = notifNoteTextView.text
content.sound = UNNotificationSound.default()
content.badge = UIApplication.shared.applicationIconBadgeNumber + 1 as NSNumber
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)
}
})
}
日期是从日期选择器中选取的 . 现在从另一个选择器我希望用户选择重复间隔(无,每天,每周,每月,每年和特定日期) . 我不确定如何实现这一目标 . 我是否需要使用if else if语句? (这对我来说似乎不太正确 . )有没有更好的方法来实现这一目标?谢谢!
1 回答
目前我已经用这种方式修复了它:
还没有找到一种方法来安排不同的时间间隔(例如每两周,每隔一天,每六个月等) . 我仍然会研究它 . :)