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?
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!
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
.
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
}
}
Another option might be to look up the EKParticipant's URL. The output should be a mailto URI like mailto:xyz@xyz.com. There's some sparse documentation here:
The property is not exposed per API version 6.0 - i'm searching for the answer myself and have not found any other work around other than parsing the email address out of the object's description. Example:
EKParticipant *organizer = myEKEvent.organizer
NSString *organizerDescription = [organizer description];
//(id) $18 = 0x21064740 EKOrganizer <0x2108c910> {UUID = D3E9AAAE-F823-4236-B0B8-6BC500AA642E; name = Hung Tran; email = hung@sampleemail.com; isSelf = 0}
Parse the above string into an NSDictionary pull the email by key @"email"
来源:https://stackoverflow.com/questions/13642786/how-to-get-ekevent-ekparticipant-email