Recurring Reminders With Swift in iOS development
In this article, we will create, update and delete a recurring reminders with Swift.
Working with Recurring Reminder is just as simple as working with Reminders in Swift . The spotlight in former case is primarily on recurrence rule.
Creating a Recurring Reminder
Recurrence Rule
Recurrence rule can be either simple weekly, daily, yearly reminder or a complex one as below:
EKRecurrenceRule(recurrenceWith: .weekly, interval: 1, daysOfTheWeek: [.init(.monday),.init(.tuesday)], daysOfTheMonth: nil, monthsOfTheYear: nil, weeksOfTheYear: nil, daysOfTheYear: nil, setPositions: nil, end: .none)
The above recurrence rule is to remind every monday and tuesday.
Similarly One can set specific months/weeks/days as well.
The setPositions parameter can be combined with any other recurrence condition. This is array further filtering the recurrence conditions.
For example, if a reminder is as follows
EKRecurrenceRule(recurrenceWith: .yearly, interval: 1, daysOfTheWeek: [.init(.monday), .init(.tuesday), .init(.wednesday), .init(.thursday), .init(.friday)], daysOfTheMonth: nil, monthsOfTheYear: nil, weeksOfTheYear: nil, daysOfTheYear: nil, setPositions: [1,-1], end: .none)
The recurrence only occurs on the first and last weekday of every year.
While Adding a recurring reminder, the following few parameters which are really important.
- dueDateComponents: Date Components of Date by which reminder should be completed. Similarly one can add startDateComponents as well. But dueDateComponents is mandatory when creating a recurring reminder.
- EKAlarm: While adding EKAlarm, it is recommended to use offset which is relative to due date.
Updating a Recurring Reminder
Updating a reminder is a simple task. But With respect to Recurring reminders, completing a reminder can be interesting.
For example if a reminder was set to run every monday. If we complete that reminder, it completes just this week monday reminder and updates immediately to next week.
Removing Recurring Reminder
Removing recurring reminder is same as removing a reminder. The below code can be used.
Thanks for reading :))