0
  1. I am setting an event for say the 13th of some month and recur it every month. It is getting repeated properly every month. But when set it for 31st of some month it gets set for months with 31st only and in event it shows in Edit->Repeat->repeats every month, each 31st.
  2. I want it to set for at least last day month if no date 31st available for that month.
  3. Also for the month of February no 29, 30, or 31 then also the event from other months with these dates are not created.

Please note that we wanted to set continuous set of events so that deleting and editing them would be easy.

I wrote the following code:

let eventstore : EKEventStore = EKEventStore()
        eventstore.requestAccess(to: EKEntityType.event, completion: { (isallow, error) -> Void in
            var event:EKEvent!

            if (event == nil) {
                event = EKEvent(eventStore: eventstore)
                event.title = self.getReminderTitleForCalendarEvent(dtDate: date)
                event.notes = ""
                event.startDate = date
                event.endDate = date
                event.calendar = eventstore.defaultCalendarForNewEvents
                let ekrules: EKRecurrenceRule = EKRecurrenceRule.init(recurrenceWith: EKRecurrenceFrequency.monthly, interval:self.selectedFrequency!, end: nil)
                event.recurrenceRules = [ekrules]
            }
            else // if there is already a event then update date
            {

                // if event is deleted by user
                event.startDate = date
                event.endDate = date
                if let oldalarm = event.alarms?[0]// remove old date alarm
                {
                    event.removeAlarm(oldalarm)
                }
                // Below code is to remove existing event
                do
                {
                    try  eventstore.remove(event, span: .thisEvent)
                    //self.objselectedinfusion.strEventId = event.eventIdentifier
                }
                catch let error as NSError {
                    //print(error.localizedDescription)
                }
            }
            let alarm = EKAlarm(relativeOffset: self.getTimeIntervalForAlert())

   let alarm = EKAlarm(absoluteDate: reminderFireDate as Date)
  event.addAlarm(alarm)

            do
            {
                try eventstore.save(event, span: EKSpan.futureEvents, commit: true)
                UserDefaults.standard.set(event.eventIdentifier, forKey: Constant.calendarEventID)
            }
            catch let error as NSError {
                //print(error.localizedDescription)
            }

        })

Here selectedFrequency is recurrence interval that is some number of months.

shim
  • 9,289
  • 12
  • 69
  • 108

2 Answers2

1
 class  func getRepeatValue (_ option : String) -> EKRecurrenceRule?{

    // ["Daily" , "Weekly" , "Monthly" ,"Yearly","None"]

    //        "daily" , "weekly" , "monthly" ,"yearly","none"

    switch option {
    case "Daily":


        let rule = EKRecurrenceRule(recurrenceWith: EKRecurrenceFrequency.daily, interval: 1, end: nil)

        //daily for 50 years
        return rule
    case "Weekly":

        //on the same week day for 50 years
        let rule = EKRecurrenceRule(recurrenceWith: EKRecurrenceFrequency.weekly, interval: 1, end: nil)


        return rule
    case "Monthly":

        //on the same date of every month
        let rule = EKRecurrenceRule(recurrenceWith: EKRecurrenceFrequency.monthly, interval: 1, end: nil)


        return rule

    case "Yearly":

        //on the same date and month of the year
        let rule = EKRecurrenceRule(recurrenceWith: EKRecurrenceFrequency.yearly, interval: 1, end: nil)


        return rule
    case "None":
        return nil
    default:
        return nil
    }
}
Akash Shindhe
  • 558
  • 4
  • 16
  • 1
    SO prefers explanations to code-only answers. Can you explain how this works and why this code solves the OP's problem? – toonarmycaptain Nov 08 '17 at 21:11
  • read this - the above ans is not complete - you have to create complex recurrence rule . https://developer.apple.com/documentation/eventkit/ekrecurrencerule/1507320-init – Akash Shindhe Nov 14 '17 at 20:21
0

In case someone still searching answer, here it is:

let rule = EKRecurrenceRule(
    recurrenceWith: .monthly,
    interval: 1,
    daysOfTheWeek: nil,
    daysOfTheMonth: [-1],
    monthsOfTheYear: nil,
    weeksOfTheYear: nil,
    daysOfTheYear: nil,
    setPositions: nil,
    end: nil
)

This rule will create an event that will occur every last date of month