how to get ekevent EKparticipant email?

前端 未结 6 761
花落未央
花落未央 2020-12-10 04:21

how to get ekevent EKparticipant email?

EKParticipant class does not have such a attribute.

Is it possible to render the native ios participants controller t

相关标签:
6条回答
  • 2020-12-10 04:53

    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.

    0 讨论(0)
  • 2020-12-10 05:03

    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"

    0 讨论(0)
  • 2020-12-10 05:07

    Based on Anton Plebanovich's answer I've made this Swift 5 solution for this made up problem Apple introduced:

    Swift 5

    private let emailSelector = "emailAddress"
    extension EKParticipant {
      var email: String? {
        if responds(to: Selector(emailSelector)) {
          return value(forKey: emailSelector) as? String
        }
    
        let emailComponents = description.components(separatedBy: "email = ")
        if emailComponents.count > 1 {
          return emailComponents[1].components(separatedBy: ";")[0]
        }
    
        if let email = (url as NSURL).resourceSpecifier, !email.hasPrefix("/") {
          return email
        }
    
        return nil
      }
    }
    
    0 讨论(0)
  • 2020-12-10 05:09

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-10 05:11

    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

    0 讨论(0)
  • 2020-12-10 05:17

    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!

    0 讨论(0)
提交回复
热议问题