访问EKParticipant / EKAttendee中的电子邮件

时间:2014-08-13 18:54:31

标签: ios ekevent ekeventkit

我想获取EKEventKit中活动参与者的电子邮件地址。

我有以下代码:

if ( event.attendees.count > 0)
{
    NSArray *people = event.attendees;
    for(EKParticipant *person in people)
    {
        if ( person.participantType == EKParticipantTypePerson && person.URL.resourceSpecifier.length > 0)
        {
            NSString *dataString = [NSString stringWithFormat:@"event_id=%ld&name=%@&is_me=%d&email=%@&role=%@",event_id,person.name, person.isCurrentUser,person.URL.resourceSpecifier, @"attendee"];
            //<DO SOMETHING USEFUL WITH dataString>;
        }
    }
}

当我运行代码时,填充以下数据:

EKAttendee <0x17809acc0> {UUID = 4F657EA4-452A-412B-A9AA-FEC5551DC096; name = A. Real Person; email = realperson@therightdomain.com; status = 0; role = 0; type = 1}

如何访问电子邮件字段?

我尝试(如上所述)使用URL.resourceSpecifier,但这通常是一些奇怪的字符串,绝对不是电子邮件地址。

3 个答案:

答案 0 :(得分:2)

EKParticipant对象的“描述”是各种属性列表。我尝试了几种不同的方法将该列表解析为包含键:值对的内容失败。所以我写了以下内容:

                            // This is re-useable code that converts any class description field into a dictionary that can be parsed for info
                    NSMutableDictionary *descriptionData = [NSMutableDictionary dictionary];
                    for (NSString *pairString in [person.description componentsSeparatedByString:@";"])
                    {
                        NSArray *pair = [pairString componentsSeparatedByString:@"="];
                        if ( [pair count] != 2)
                            continue;
                        [descriptionData setObject:[[pair objectAtIndex:1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] forKey:[[pair objectAtIndex:0]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
                    }

有了这个,我只需使用

获取电子邮件地址
    [descriptionData valueForKey:@"email"]

答案 1 :(得分:1)

我试图在"how to get ekevent EKparticipant email?"主题中回答同样的问题:

您需要做的是使用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 :(得分:0)

在Swift 4中:

private static func getParticipantDescriptionData(_ participant: EKParticipant) -> [String:String] {
  var descriptionData = [String: String]()
  for pairString in participant.description.components(separatedBy: ";") {
    let pair = pairString.components(separatedBy: "=")
    if pair.count != 2 {
      continue
    }
    descriptionData[pair[0].trimmingCharacters(in: .whitespacesAndNewlines)] =
      pair[1].trimmingCharacters(in: .whitespacesAndNewlines)
  }
  return descriptionData
}