Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get ekevent EKparticipant email?

Tags:

ekevent

how to get ekevent EKparticipant email?

EKParticipant class does not have such a attribute.

Is it possible to render the native ios participants controller to show the list of participants?

like image 292
Evan Lee Avatar asked Nov 30 '12 09:11

Evan Lee


4 Answers

I had this same question and when I was at WWDC this year, I asked several Apple engineers and they had no clue. I asked a guy I met in line and he had the answer:

event.organizer.URL.resourceSpecifier

This works for any EKParticipant. I was cautioned NOT to use the description field because that may change at any time.

Hope this helps!

like image 64
Moebius Avatar answered Nov 16 '22 23:11

Moebius


Category for EKParticipant:

import Foundation
import EventKit
import Contacts

extension EKParticipant {
    var email: String? {
        // Try to get email from inner property
        if respondsToSelector(Selector("emailAddress")), let email = valueForKey("emailAddress") as? String {
            return email
        }

        // Getting info from description
        let emailComponents = description.componentsSeparatedByString("email = ")
        if emailComponents.count > 1 {
            let email = emailComponents[1].componentsSeparatedByString(";")[0]
            return email
        }

        // Getting email from contact
        if let contact = (try? CNContactStore().unifiedContactsMatchingPredicate(contactPredicate, keysToFetch: [CNContactEmailAddressesKey]))?.first,
            let email = contact.emailAddresses.first?.value as? String {
            return email
        }

        // Getting email from URL
        if let email = URL.resourceSpecifier where !email.hasPrefix("/") {
            return email
        }

        return nil
    }
}
like image 43
Anton Plebanovich Avatar answered Nov 16 '22 23:11

Anton Plebanovich


None of the above solutions are reliable:

  1. URL may be something like /xyzxyzxyzxyz.../principal and obviously that's not an email.
  2. EKParticipant:description may change and not include email anymore.
  3. You could send emailAddress selector to the instance but that's undocumented, may change in the future and in the meantime might get your app disapproved.

So at the end what you need to do is use EKPrincipal:ABRecordWithAddressBook and then extract email from there. Like this:

NSString *email = nil;
ABAddressBookRef book = ABAddressBookCreateWithOptions(nil, nil);
ABRecordRef record = [self.appleParticipant ABRecordWithAddressBook:book];
if (record) {
    ABMultiValueRef value = ABRecordCopyValue(record, kABPersonEmailProperty);
    if (value
        && ABMultiValueGetCount(value) > 0) {
        email = (__bridge id)ABMultiValueCopyValueAtIndex(value, 0);
    }
}

Note that calling ABAddressBookCreateWithOptions is expensive so you might want to do that only once per session.

If you can't access the record, then fall back on URL.resourceSpecifier.

like image 5
ierceg Avatar answered Nov 16 '22 22:11

ierceg


Another option might be to look up the EKParticipant's URL. The output should be a mailto URI like mailto:[email protected]. There's some sparse documentation here:

http://developer.apple.com/library/ios/#documentation/EventKit/Reference/EKParticipantClassRef/Reference/Reference.html

like image 2
Rahul Jaswa Avatar answered Nov 16 '22 21:11

Rahul Jaswa