Modifying Ekparticipants (Attendees) in Eventkit Like Sunrise

Modifying EKParticipants (attendees) in EventKit like Sunrise

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:@"test@email.com" 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 {
NSLog(@"Success!");
}

Adding EKParticipants to an EKEvent in EventKit

If you want to add attendees to an EKEvent, you must implement EKEventKitUI, and use the view controllers of that framework, this framework provide the calendar native like views so you can add attendees for your EKEvent.

But you can't add attendees to EKEvent programmatically since the attendees is a read- only property, so you ca't set it's value by code, and since you ca't create EKParticipant object directly by code.

may be this limitation change in coming versions of iOS.

EventKit : Is there any way to remove Invitees from EKEventEditViewController

I presume your talking about the displaying of the invitees and repeat options
from EKEventEditViewController?

Having looked at the current API for EKEventEditViewController it doesn't look like there is a way.

However, you could create a custom view controller that replicated the look of the EKEventEditViewController minus the event options you are not interested. You'd need to construct the interface, code the view controller and a delegate interface. It's more effort than using EKEventEditViewController but I can't see you achieving what you want any other way.



Related Topics



Leave a reply



Submit