kbunarjo kbunarjo - 5 days ago 5
iOS Question

How to repeat local notifications on specific days of the week (iOS Swift 3)

I am creating my first app that lets the user set alerts for themselves and then the app would send the user a local notification on the set time. The thing that I'm having trouble with now is being able to fire the local notification on specific days of the week (for example every Mondays and Tuesdays only). All the tutorials/questions that I have come across so far (like this one) have only been about scheduling the notification everyday/everyweek. The desired result that I'm trying to achieve is sort of like the iPhone's built in alarm system, where you can set an alarm to fire only on certain days of the week.

If I can't just set the

notification.repeatInterval
field, is there a way I can do what I'm trying to do? Could I possibly execute a little code at midnight that schedules the correct notifications for that day? If so, how could I do that?

Thanks in advance!

Answer

The thing that I'm having trouble with now is being able to fire the local notification on specific days of the week

In iOS 10, a UNCalendarNotificationTrigger is formed using DateComponents. This means you can specify as much or as little of the date-time in question as you wish. Thus, if you specify a specific weekday and a time (hour and minutes), and nothing else, you'll repeat at that time on that day of the week.

If you also need to repeat on a different day of the week, just make another notification.

Comments