0

I have the present method to allow for creating a calendar event within EventKit.

func createEvent(eventStore: EKEventStore, title: String, startDate: NSDate, endDate: NSDate) {
    let event = EKEvent(eventStore: eventStore)

    event.title = title
    event.startDate = startDate
    event.endDate = endDate
    event.calendar = eventStore.defaultCalendarForNewEvents

    do {
        try eventStore.saveEvent(event, span: .ThisEvent)
        savedEventId = event.eventIdentifier
    } catch {
        print("Bad things happened")
    }
}

However, I wish to allow users to select several days of the week to create a recurring event. For example, a user sets time of 10:05am, and selects Monday, Wednesday, Thursday. How can I formulate EventKit to allow for this event to schedule on the users calendar properly?

Sauron
  • 6,399
  • 14
  • 71
  • 136

2 Answers2

2

I see that the timestamp on your post is way back in May so you probably don't need the help anymore, but for anyone else who stumbles across this question, you'll have to utilize the EKRecurrenceRule class and attach it to the event.

Here's an example in Objective-C of using it to schedule an event for every Friday (like your scenario):

// The EKRecurrenceRule accepts an NSArray of day objects, I did this to make adding multiple days easier
NSMutableArray *daysArr = [[NSMutableArray alloc] init];

// Days of the week are ordered Sunday to Saturday, 1-7
EKRecurrenceDayOfWeek *theDay = [[EKRecurrenceDayOfWeek alloc] initWithDayOfTheWeek:6 weekNumber:0];

// Add Friday to the array
[daysArr addObject:theDay];

// Instantiate the EKRecurrenceRule, giving it a weekly frequency and the day of the week
EKRecurrenceRule *recurrenceRule = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:EKRecurrenceFrequencyWeekly interval:1 daysOfTheWeek:daysArr daysOfTheMonth:nil monthsOfTheYear:nil weeksOfTheYear:nil daysOfTheYear:nil setPositions:nil end:nil];

// Apply it to the event
[event setRecurrenceRules:@[recurrenceRule]];

I hope this helps someone out!

0

I know this might also be late, but at least it works with Swift and not ObjC. I made two functions, one that creates Date events in the bottom, and another one that creates recurring events called addEventToCalendar. Note that you still need to clarify which day of the week that specific event is.

If you still have doubts on how events work I suggest consulting this link.

func addEventToCalendar(title: String, description: String?, startDate: Date, endDate: Date, completion: ((_ success: Bool, _ error: NSError?) -> Void)? = nil){
        let eventStore = EKEventStore()

        eventStore.requestAccess(to: .event, completion: { (granted, error) in
            if (granted) && (error == nil) {
                let event = EKEvent(eventStore: eventStore)
                event.title = title
                event.startDate = startDate
                event.endDate = startDate
                event.notes = description
                event.calendar = eventStore.defaultCalendarForNewEvents

                let recurrenceRule = EKRecurrenceRule.init(
                    recurrenceWith: .daily,
                    interval: 1,
                    daysOfTheWeek: [EKRecurrenceDayOfWeek.init(EKWeekday.saturday)],
                    daysOfTheMonth: nil,
                    monthsOfTheYear: nil,
                    weeksOfTheYear: nil,
                    daysOfTheYear: nil,
                    setPositions: nil,
                    end: EKRecurrenceEnd.init(end:endDate)
                )

                event.recurrenceRules = [recurrenceRule]
                do {
                    try eventStore.save(event, span: .thisEvent)
                } catch let e as NSError {
                    completion?(false, e)
                    return
                }
                completion?(true, nil)
            } else {
                completion?(false, error as NSError?)
            }
        })
    }

    private func createDate(date: String!, hours: Int!, minutes:Int! ) -> Date{
        var fullDateArr = date.components(separatedBy: "-")

        let startMonth: Int? = Int(fullDateArr[0])!
        let startDay: Int? = Int(fullDateArr[1])!
        let startYear: Int = Int(fullDateArr[2])!


        var dateComponents = DateComponents()
        dateComponents.year = startYear
        dateComponents.month = startMonth
        dateComponents.day = startDay
        dateComponents.timeZone = TimeZone(abbreviation: "MDT") // ELP Standard Time
        dateComponents.hour = hours
        dateComponents.minute = minutes

        let userCalendar = Calendar.current // user calendar
        let someDateTime = userCalendar.date(from: dateComponents)

        return someDateTime!
    }
Jmz
  • 159
  • 7