如何获得ekevent EKparticipant电子邮件?
EKParticipant类没有这样的属性.
是否可以呈现本机ios参与者控制器以显示参与者列表?
我有同样的问题,当我今年去WWDC时,我问了几位Apple工程师他们没有任何线索.我问了一个我遇到的人,他得到了答案:
event.organizer.URL.resourceSpecifier
Run Code Online (Sandbox Code Playgroud)
这适用于任何EKParticipant.我被警告不要使用描述字段,因为这可能随时改变.
希望这可以帮助!
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
}
}
Run Code Online (Sandbox Code Playgroud)
上述解决方案都不可靠:
URL
可能类似于/xyzxyzxyzxyz.../principal
电子邮件,但显然这不是电子邮件。EKParticipant:description
可能会发生变化并且不再包含电子邮件。emailAddress
选择器发送到实例,但这没有记录,将来可能会发生变化,同时您的应用程序可能会被拒绝。所以最后你需要做的是使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,拨打电话的ABAddressBookCreateWithOptions
费用很高,因此您可能只想在每个会话中拨打一次。
如果您无法访问该记录,请依靠URL.resourceSpecifier
。