My goal is to add some invitees to an EKEvent. I\'ve looked into other questions like this, such as this one, but all say that it is essentially impossible to a
Swift version of rebello95's solution. I can confirm it works like a charm! I played around with the firstName and lastName properties but they do not seem to matter at all, so I left them out of my example. Only the emailAddress matters, which is matched against your contact list to display a full name in for example the native calendar app.
private func createEventWithAttendees() {
let eventStore = EKEventStore()
let calendars = eventStore.calendarsForEntityType(.Event)
let event = EKEvent(eventStore: eventStore)
event.title = "TEST EVENT"
event.startDate = NSDate()
event.endDate = NSDate().dateByAddingTimeInterval(60 * 60)
event.calendar = calendars[0]
var attendees = [EKParticipant]()
for i in 0 ..< 5 {
if let attendee = createParticipant(email: "test\(i)@email.com") {
attendees.append(attendee)
}
}
event.setValue(attendees, forKey: "attendees")
try! eventStore.saveEvent(event, span: .ThisEvent)
}
private func createParticipant(email email: String) -> EKParticipant? {
let clazz: AnyClass? = NSClassFromString("EKAttendee")
if let type = clazz as? NSObject.Type {
let attendee = type.init()
attendee.setValue(email, forKey: "emailAddress")
return attendee as? EKParticipant
}
return nil
}