0

我想寫一些代碼,它將允許用戶設置自定義本地通知來提醒他們一些任務。 我正在使用用戶通知框架。 因此,基本上用戶可以決定設置一個通知,它會提醒他每週購買一些肥皂,或每年製作生日祝福或每月檢查一些東西(這些僅僅是示例)。在用戶選擇的swift 3重複間隔中的本地通知

到目前爲止,我已經能夠設置本地通知沒有重複的間隔。

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 if語句? (它對我來說看起來不太正確。) 有沒有更好的方法來實現這個目標? 謝謝!

回答

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 
...... 

還沒有找到又一個的方式以安排不同的時間間隔(如每隔兩週,每隔一天,每半年等) 。 我仍會研究它。 :)

相關問題