0

我正在尝试编写一些代码,允许用户设置自定义本地通知以提醒他们一些任务。我正在使用用户通知框架。所以基本上用户可以决定设置一个通知,提醒他每周买一些肥皂或每年做生日祝福或每月检查一些东西(这些只是示例)。

到目前为止,我已经能够设置没有重复间隔的本地通知。

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 语句吗?(这对我来说似乎并不正确。)有没有更好的方法来实现这一点?谢谢!

4

1 回答 1

0

目前我已经这样修复它:

let dateformatter = DateFormatter()
dateformatter.dateStyle = .medium
dateformatter.timeStyle = .short
let dateFromString = dateformatter.date(from: selectDateTextField.text!)
let fireDateOfNotification: Date = dateFromString!

var trigger: UNCalendarNotificationTrigger
var triggerDate: DateComponents
var repeatInterval = Bool()

if repeatingInterval == "Hourly" {
    triggerDate = Calendar.current.dateComponents([.minute], from: fireDateOfNotification)
    repeatInterval = true
} else if repeatingInterval == "Daily" {
    triggerDate = Calendar.current.dateComponents([.hour, .minute], from: fireDateOfNotification)
    repeatInterval = true
} else if repeatingInterval == "Weekly" {
    triggerDate = Calendar.current.dateComponents([.weekday, .hour, .minute], from: fireDateOfNotification)
    repeatInterval = true
} else if repeatingInterval == "Monthly" {
    triggerDate = Calendar.current.dateComponents([.day, .hour, .minute], from: fireDateOfNotification)
    repeatInterval = true
} else if repeatingInterval == "Yearly" {
    triggerDate = Calendar.current.dateComponents([.month, .day, .hour, .minute], from: fireDateOfNotification)
    repeatInterval = true
} else {
    triggerDate = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute,.second,], from: fireDateOfNotification)
    repeatInterval = false
}

trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: repeatInterval)

//Schedule the Notification
......

还没有找到一种方法来安排不同的时间间隔(例如每两周、每两天、每六个月等)。我仍然会调查它。:)

于 2017-09-06T17:11:32.920 回答