Search code examples
swifteventkitekeventekeventstore

How to get the travelTime of an EKEvent instance


I'd like to get the travel time of a calender event (EKEvent), but I get this error: Value of type 'EKEvent' has no member 'travelTime'.

When I print an EKEvent, I get this:

Screenshot of console showing an EKEvent with travelTime

EKEvent <0x79f6f460>
{
     EKEvent <0x79f6f460>
{    title =        Test; 
     location =     Foobar; 
     calendar =     EKCalendar <0x7ba6f9a0> {title = Calendar; type = Local; allowsModify = NO; color = #1BADF8;}; 
     alarms =       (null); 
     URL =          (null); 
     lastModified = 2016-04-25 17:56:58 +0000; 
     startTimeZone =    Europe/Amsterdam (GMT+2) offset 7200 (Daylight); 
     startTimeZone =    Europe/Amsterdam (GMT+2) offset 7200 (Daylight) 
}; 
     location =     Foobar; 
     structuredLocation =   EKStructuredLocation <0x79e722e0> {title = Foobar; address = (null); geo = (null); abID = (null); routing = (null); radius = 0.000000;}; 
     startDate =    2016-04-25 19:15:00 +0000; 
     endDate =      2016-04-25 20:15:00 +0000; 
     allDay =       0; 
     floating =     0; 
     recurrence =   (null); 
     attendees =    (null); 
     travelTime =   30 minutes; 
     startLocation =    (null);
};

Does anyone have an idea about what's going on? (I can access stuff like the title of startDate just fine)


Solution

  • This seems to return the travel time property in terms of seconds:

     let travelTime = yourEKEvent.valueForKey("travelTime")!
    

    Apparently there is no accessible travelTime property in the EKEvent class so i guess its somewhere in its super classes. In the built in iOS calendar when you add travel time to an event the detail to the option reads:

    Event alerts will take this time into account and your calendar will be blocked during this time

    enter image description here

    After looking at theEKAlarms documentation I think it may be stored in the relativeOffset property. The alarms should be stored in the EKCalendar super class.

    You can also update the value by the set value method:

    yourEKEvent.setValue(300, forKey: "travelTime")

    and then re-save the event using a method like this:

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