Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在EKParticipant/EKAttendee中访问电子邮件_Ios_Ekevent_Ekeventkit - Fatal编程技术网

Ios 在EKParticipant/EKAttendee中访问电子邮件

Ios 在EKParticipant/EKAttendee中访问电子邮件,ios,ekevent,ekeventkit,Ios,Ekevent,Ekeventkit,我想在EKEventKit中获取活动参与者的电子邮件地址 我有以下代码: if ( event.attendees.count > 0) { NSArray *people = event.attendees; for(EKParticipant *person in people) { if ( person.participantType == EKParticipantTypePerson && person.URL.resou

我想在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>;
        }
    }
}
if(event.attendes.count>0)
{
NSArray*人=事件。参与者;
for(EKParticipant*人与人之间的人)
{
if(person.participantType==EKParticipantTypePerson&&person.URL.resourceSpecifier.length>0)
{
NSString*dataString=[NSString stringWithFormat:@“事件id=%ld&name=%@&is_me=%d&email=%@&role=%@”,事件id,person.name,person.isCurrentUser,person.URL.resourceSpecifier,@“与会者”];
//;
}
}
}
运行代码时,person会填充以下数据:

EKAttendee <0x17809acc0> {UUID = 4F657EA4-452A-412B-A9AA-FEC5551DC096; name = A. Real Person; email = realperson@therightdomain.com; status = 0; role = 0; type = 1}
ekadenee{UUID=4F657EA4-452A-412B-A9AA-FEC55551DC096;name=A.真人;电子邮件=realperson@therightdomain.com;状态=0;角色=0;类型=1}
如何访问电子邮件字段?

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

EKParticipant对象的“描述”是一个排序属性列表。我尝试了几种不同的方法将该列表解析为包含key:value对的内容,但都没有成功。所以我写了以下内容:

                            // 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"]
EKParticipant对象的“描述”是一个排序属性列表。我尝试了几种不同的方法将该列表解析为包含key:value对的内容,但都没有成功。所以我写了以下内容:

                            // 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"]

我试图在帖子中回答同样的问题:

您需要做的是使用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

我试图在线程中回答相同的问题:

您需要做的是使用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
代价很高,因此您可能希望每个会话只调用一次

如果无法访问该记录,请使用Swift 4中的
URL.resourceSpecifier

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

回答得很好,但当参与者不在iPhone的通讯录中时,此记录将为零。@frank谢谢。是的,这通常是一个不可靠的API。我想我们最终决定将
电子邮件地址
选择器(未记录)发送到
appleParticipant
@frank。这应该不会有危险(数据丢失、崩溃等),但这绝对是一种肮脏的技术。回答得好,但当参与者不在iPhone的通讯簿中时,此记录将为零。@frank谢谢。是的,这通常是一个不可靠的API。我想我们最终决定将
电子邮件地址
选择器(未记录)发送到
appleParticipant
@frank它应该不会有危险(数据丢失、崩溃等),但这绝对是一种肮脏的技术。它如预期般工作,我在iOS 12中遇到问题,无法获取电子邮件字段,你的代码对我有效。它按预期工作,我在iOS 12中遇到了一个问题,无法获取电子邮件字段,您的代码对我有效。