1

I have a function that adds a new reminder. In this function, the reminder will have a reminder title and an alarm for a specific day and time.

The function currently looks like this:

var eventStore = EKEventStore()
var reminders = [EKReminder]()

func addReminder(date: Date, reminderTitle: String) {
    eventStore.requestAccess(to: EKEntityType.reminder, completion: {
        granted, error in
        if (granted) && (error == nil) {
            let reminder:EKReminder = EKReminder(eventStore: self.eventStore)
            reminder.title = reminderTitle

            let alarmTime = date
            let alarm = EKAlarm(absoluteDate: alarmTime)
            reminder.addAlarm(alarm)
            reminder.calendar = self.eventStore.defaultCalendarForNewReminders()

            do {
                try self.eventStore.save(reminder, commit: true)
            } catch {
                print("Cannot save")
                return
            }
            print("Reminder saved")
        }
    })
}

Now, I would like to add an EKRecurrenceRule that will repeat the alarm/reminder on a weekly basis forever (without a specific end date).

For a better explanation, this is the example of how I want my reminder to look in the end: Example

This is the approach I have already tried:

let recurrenceRule = EKRecurrenceRule(recurrenceWith: .weekly, interval: 1, daysOfTheWeek: [EKRecurrenceDayOfWeek(.monday)], daysOfTheMonth: nil, monthsOfTheYear: nil, weeksOfTheYear: nil, daysOfTheYear: nil, setPositions: nil, end: nil)
reminder.addRecurrenceRule(recurrenceRule)

Basically this should repeat the reminder every Monday, but for some reason, I cannot save the reminder.

Any help on how to successfully save a reminder?

1 Answers1

1

SOLUTION: In order to have a working recurring event/reminder, the reminder needs to have a defined due date.

if(reminder.recurrenceRules?.count ?? 0 > 0 && reminder.dueDateComponents == nil) {
     let calendar = Calendar.current
     let components = calendar.dateComponents([Calendar.Component.day, Calendar.Component.month, Calendar.Component.year], from: date)
     reminder.dueDateComponents = components
}
  • I just confirmed this worked for me, and also that it did not work without having a due date. EKEvent always requires an end date, but EKReminder does not require any dates, so requiring an end date is a must if you want to add recurrence rules to either. – Michael Ellis Jan 24 '22 at 04:07