rebello95 rebello95 - 1 year ago 146
iOS Question

Modifying EKParticipants (attendees) in EventKit like Sunrise

My goal is to add some invitees to an

. I've looked into other questions like this, such as this one, but all say that it is essentially impossible to add
s to an
programmatically. I do see that the
property is read-only, but I see other services like Sunrise using it in their mobile app.

I'm confident that they're using some system that at least partially integrates with EventKit because they're pulling in calendars from the user's iCal app. Invites sent from an added Exchange account, for example, are also clearly sent by the Exchange service as opposed to Sunrise's own invite (a result of either posting the event straight to Exchange or to posting it to iCal).

Any workaround this restriction would be very helpful - maybe an endpoint on Exchange that can be called to add/remove invitees? A workaround inside of EventKit? As long as it's not a private Apple API, I'd be more than happy to try it out.


Answer Source

I figured it out!

Essentially, one must go into the EKAttendee class, which inherits from EKParticipant. I did this by creating a generic instance of that class using the NSClassFromString() method.

Once you have an attendee, you can set the properties using the setValue:ForKey: function.

Lastly, compile your EKAttendee instances into an array, and set that to the EKEvent's attendees property.

I tested this with an Exchange account on my device, and it worked like a charm - invite sent and everything!

The code below is what I'm now using to set the attendees of events. My example is for creating new events, but this can be done very simply for existing events by making a copy of the attendees list on the EKEvent, then modifying that and re-setting it.

//Create generic event info
EKEvent *event = [EKEvent eventWithEventStore:database];
event.title = @"TEST EVENT";
event.location = @"Test location";
event.notes = @"Example notes";
event.startDate = [NSDate date];
event.endDate = [[NSDate date] dateByAddingTimeInterval:(60 * 60)];
event.calendar = exchange;

//Do our super clever hack
NSMutableArray *attendees = [NSMutableArray new];
for (int i = 0; i < 5; i++) {

    //Initialize a EKAttendee object, which is not accessible and inherits from EKParticipant
    Class className = NSClassFromString(@"EKAttendee");
    id attendee = [className new];

    //Set the properties of this attendee using setValue:forKey:
    [attendee setValue:@"Invitee" forKey:@"firstName"];
    [attendee setValue:[NSString stringWithFormat:@"#%i", i + 1] forKey:@"lastName"];
    [attendee setValue:@"" forKey:@"emailAddress"];

    //Add this attendee to a list so we can assign it to the event
    [attendees addObject:attendee];

//Finally, add the invitees to the event
[event setValue:attendees forKey:@"attendees"];

//Save the event!
NSError *error = nil;
[database saveEvent:event span:EKSpanThisEvent error:&error];
if (error) {
    NSLog(@"Save failed with error: %@", error);
} else {