ekeventekrecurrencerule

Calculate occurrence dates from EKRecurrenceRule


Given an EKRecurrenceRule, a start date and a range of dates, how to get a list of occurrence dates that fall within that range?


Solution

  • Here’s a general approach:
    
    Step-by-step (in Swift):
    
    1. Create an EKEvent with a recurrence rule.
    
    
    2. Use EKEventStore to fetch events in a date range, which will include the recurring ones.
    
    
    
    Example in Swift:
    
    import EventKit
    
    let store = EKEventStore()
    store.requestAccess(to: .event) { (granted, error) in
        if granted {
            let calendars = store.calendars(for: .event)
            let startDate = Date()
            let endDate = Calendar.current.date(byAdding: .year, value: 1, to: startDate)!
    
            let predicate = store.predicateForEvents(withStart: startDate, end: endDate, calendars: calendars)
    
            let events = store.events(matching: predicate)
            
            let recurringEvents = events.filter { $0.recurrenceRules != nil }
            
            for event in recurringEvents {
                print("Event: