I'm trying to write some code which will allows users to set custom local notifications to remind them some tasks. I'm using the user notification framework. So basically a user can decide to set a notification which will remind him to buy some soap every week or to make birthday wishes every year or to check something every month (these are just examples).
So far I've been able to set a local notification with no repeating interval.
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)
}
})
}
The date is picked from a date picker. Now from another picker i would like the user to select the repeating interval (none, daily, weekly, monthly, yearly and on specific days). I'm not sure how I can achieve this. Do I need to use an if else if statement? (it doesn't seems really correct to me.) Is there a better way to achieve this? Thank you!
For the moment I've fixed it this way:
Haven't find yet a way to schedule it for different interval (e.g. every two weeks, every second day, every six months etc). I will still look into it. :)