如何获得ekevent EKparticipant电子邮件?

时间:2012-11-30 09:56:33

标签: ekevent

如何获得ekevent EKparticipant电子邮件?

EKParticipant类没有这样的属性。

是否可以呈现本机ios参与者控制器以显示参与者列表?

6 个答案:

答案 0 :(得分:9)

我有同样的问题,当我今年去WWDC时,我问了几位Apple工程师他们没有任何线索。我问了一个我遇到的人,他得到了答案:

event.organizer.URL.resourceSpecifier

这适用于任何EKParticipant。我被警告不要使用描述字段,因为这可能随时改变。

希望这有帮助!

答案 1 :(得分:4)

以上解决方案都不可靠:

  1. URL可能类似/xyzxyzxyzxyz.../principal,显然不是电子邮件。
  2. EKParticipant:description可能会更改,不再包含电子邮件。
  3. 您可以将emailAddress选择器发送到该实例,但该文档未记录,将来可能会更改,同时可能会导致您的应用被拒登。
  4. 所以最后您需要做的是使用EKPrincipal:ABRecordWithAddressBook,然后从那里提取电子邮件。像这样:

    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);
        }
    }
    

    请注意,调用ABAddressBookCreateWithOptions的费用很高,因此您可能只想在每个会话中执行一次。

    如果您无法访问该记录,请返回URL.resourceSpecifier

答案 2 :(得分:4)

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
    }
}

答案 3 :(得分:2)

另一种选择可能是查找EKParticipant的URL。输出应该是mailto URI,如mailto:xyz@xyz.com。这里有一些稀疏的文档:

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

答案 4 :(得分:1)

每个API版本6.0都没有公开该属性 - 我自己正在搜索答案,除了从对象的描述中解析电子邮件地址之外,还没有找到任何其他工作。例如:

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} 

将上述字符串解析为NSDictionary,通过密钥@“email”

来提取电子邮件

答案 5 :(得分:0)

基于Anton Plebanovich的回答,我针对Apple引入的这一已解决问题做出了Swift 5解决方案:

雨燕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
  }
}
相关问题