how to get ekevent EKparticipant email?

ぐ巨炮叔叔 提交于 2019-11-28 10:09:10

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:

  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.

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:

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

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"

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!