地址簿 - 选择链接联系人的电子邮件
-
26-12-2019 - |
题
我正在尝试在下面提到的委托回调中获取所选的电子邮件属性
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
if (property==kABPersonEmailProperty) {
ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
if (ABMultiValueGetCount(emails) > 0) {
NSString *email = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(emails, ABMultiValueGetIndexForIdentifier(emails,identifier));
[recipientEmail setText:email];
[peoplePicker dismissViewControllerAnimated:YES completion:nil];
}
CFRelease(emails);
}
return NO;
}
.
但如果我选择链接联系人的电子邮件属性(有单个电子邮件),我将标识符获得为0,因此我得到了主要联系人的第一个电子邮件ID。 例如:John - John@gmail.com john26@gmail.com. Roger(Linked Contact) - roger@gmail.com
当我选择roger@gmail.com时,我会得到约翰@ gmail.com。
解决方案
我有同样的问题。当我选择具有多个电子邮件地址的帐户时,我会得到错误的电子邮件。当我循环通过选定的abmutablemultivalueref ...
for (CFIndex ix = 0; ix < ABMultiValueGetCount(emails); ix++) {
CFStringRef label = ABMultiValueCopyLabelAtIndex(emails, ix);
CFStringRef value = ABMultiValueCopyValueAtIndex(emails, ix);
NSLog(@"I have a %@ address: %@", label, value);
if (label) CFRelease(label);
if (value) CFRelease(value);
}
.
......我得到了多个相同的地址发生,但有些用空标签。
> I have a (null) address: john@home.com
> I have a _$!<Work>!$_ address: jsmith@work.com
> I have a _$!<Home>!$_ address: john@home.com
.
解决方法我将尝试首先过滤掉空标签,看看ABMultivalueIdentifier'适合',如果不恢复为NULL标签。除非你找到了什么?
编辑:这适用于我。
NSMutableArray *labeled = [NSMutableArray new];
ABMutableMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
for (CFIndex ix = 0; ix < ABMultiValueGetCount(emails); ix++) {
CFStringRef label = ABMultiValueCopyLabelAtIndex(emails, ix);
CFStringRef value = ABMultiValueCopyValueAtIndex(emails, ix);
if (label != NULL) {
[labeled addObject:(NSString *)CFBridgingRelease(value)];
}
if (label) CFRelease(label);
if (value) CFRelease(value);
}
NSString *email;
if (labeled.count > identifier) {
email = [labeled objectAtIndex:identifier];
} else {
CFStringRef emailRef = ABMultiValueCopyValueAtIndex(emails, identifier);
email = (NSString *)CFBridgingRelease(emailRef);
}
. 不隶属于 StackOverflow