0

I followed this link to create an EKAttendee object with a contact email. But when I create the event with an attendee, the UUID of the attendee is null and the attendee can't be seen in iCal.

So how can I add the UUID to my EKAtttendee object? I tried with the following code:

func createParticipant(email: String, firstName: String, familyName: String, emailIdentifier: String) -> EKParticipant? {
    let clazz: AnyClass? = NSClassFromString("EKAttendee")

    if let type = clazz as? NSObject.Type {
        let attendee = type.init()
        attendee.setValue(emailIdentifier, forKey: "identifier")
        attendee.setValue(email, forKey: "emailAddress")
        attendee.setValue(firstName, forKey: "firstName")
        //attendee.setValue(familyName, forKey: "familyName")
        return attendee as? EKParticipant
    }
    return nil
}

But the result is:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<EKAttendee 0x282db7040> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key identifier.

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

1

It turns out to be actually quit simple:

attendee.setValue(UUID().uuidString, forKey: "UUID")

You can use this handy extension to create participants from email addresses:

extension EKParticipant
{
    static func fromEmail(_ email: String) -> EKParticipant?
    {
        let clazz: AnyClass? = NSClassFromString("EKAttendee")
        if let type = clazz as? NSObject.Type
        {
            let attendee = type.init()
            attendee.setValue(email, forKey: "emailAddress")
            attendee.setValue(UUID().uuidString, forKey: "UUID")
            return attendee as? EKParticipant
        }
        return nil
    }
}
Burgler-dev
  • 230
  • 3
  • 7