Abdelrahman
Abdelrahman

Reputation: 1027

Is there any way to create hourly RecurrenceRule?

I have an EKEvent that occurs every 4 hours, and I want to create a single recurring event.

Unfortunately the available frequencies in the EKRecurrenceRule class are daily, weekly, monthly, and yearly.

How can I set hourly frequency for the EKRecurrenceRule object ?

Upvotes: 2

Views: 571

Answers (1)

Dustin Spengler
Dustin Spengler

Reputation: 7741

Unfortunately there is no way to make hourly events using the EKRecurrenceRule objects, but you can still write a method which would do the same.

    func createHourlyRecurringEvent(eventTitle: String, startDate: NSDate, endDate: NSDate, endReccurenceDate: NSDate, allDay : Bool, recurrenceSecondsInterval: Double, alarms: [EKAlarm]? , travelTime: Int?) {

    var eventStartDate = startDate
    var eventEndDate = endDate

    while eventStartDate.earlierDate(endReccurenceDate) != endReccurenceDate  {
        let event = EKEvent(eventStore: self.eventStore)

        event.title = eventTitle
        event.startDate = eventStartDate
        event.endDate = eventEndDate
        event.allDay = allDay
        event.alarms = alarms
        event.calendar = eventStore.defaultCalendarForNewEvents

        if let travelTime = travelTime {
            event.setValue(travelTime, forKey: "travelTime")
        }

        do {
            try eventStore.saveEvent(event, span: .ThisEvent)
            print("Hourly event created)")
            print("start date: \(formatter.stringFromDate(event.startDate)")
            print("End date: \(formatter.stringFromDate(event.endDate))")
        } catch {
            print("Event failed to save")
        }
        eventStartDate = NSDate(timeInterval: recurrenceSecondsInterval, sinceDate: event.startDate)
        eventEndDate = NSDate(timeInterval: recurrenceSecondsInterval, sinceDate: event.endDate)
    }
}

This code sits inside my Schedule class. After creating an instance of the schedule I can create an hourly recurring event like this:

 let startDate = NSDate()
 let endDate = NSDate(timeInterval: 10800, sinceDate: startDate)
 let oneWeekInSeconds = 604800.0
 let endRecurrenceDate = NSDate(timeInterval: oneWeekInSeconds, sinceDate: NSDate())
 let fourHours = 14400.0
 schedule!.createHourlyRecurringEvent("Testing",
                                         startDate: startDate,
                                         endDate: endDate,
                                         endReccurenceDate: endRecurrenceDate,
                                         allDay: false,
                                         recurrenceSecondsInterval: fourHours,
                                         alarms: nil,
                                         travelTime: nil)

Deleting the individual events is simple, like so:

    func deleteEvent(eventToRemove: EKEvent) -> Bool {
    var successful = false
        do {
            print("deleting single event")
            try self.eventStore.removeEvent(eventToRemove, span: .ThisEvent)
            successful = true
        } catch {
            print("Bad things happened")
        }

    return successful
}

I've yet to find a good way to batch delete all the events because they all have different unique IDs. Ill update this post when I find a good way.

Upvotes: 2

Related Questions