how to get ekevent EKparticipant email?
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
}
}
None of the above solutions are reliable:
URL
may be something like/xyzxyzxyzxyz.../principal
and obviously that's not an email.EKParticipant:description
may change and not include email anymore.- 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
.
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!
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