4

我正在尝试获取选定的手机号码

ABMultiValueRef phones = ABRecordCopyValue(person, property);
CFStringRef phoneNumber = ABMultiValueCopyValueAtIndex(phones, identifier);

我与几部手机有联系(都标有“手机”)。当我选择第一个时,phoneNumber 给我第一个,但如果我选择任何连续的,phoneNumber 给我前一个号码:

联系人:Jay Jaymes 手机 +1111111111 手机 +2222222222 手机 +3333333333

点击第一个,phoneNumber= +1111111111

点击第二个,phoneNumber= +1111111111

点击第三个,phoneNumber= +2222222222

4

4 回答 4

4

这是我使用的代码。它只会给出正确的电话号码

  - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {

        if (property == kABPersonPhoneProperty) {

            ABMultiValueRef phoneProperty = ABRecordCopyValue(person,property);
            CFIndex peopleIndex = ABMultiValueGetIndexForIdentifier(property, identifier);
            NSString *phone = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneProperty, peopleIndex);

            [self dismissModalViewControllerAnimated:YES];
        }
    return NO;
}
于 2013-11-22T10:16:35.937 回答
0

您不应该将identifier用作 中的索引ABMultiValueCopyValueAtIndex。您应该调用ABMultiValueGetIndexForIdentifierABMultiValueIdentifier标识符转换为CFIndex索引:

ABMultiValueRef phones = ABRecordCopyValue(person, property);
CFIndex index = ABMultiValueGetIndexForIdentifier(phones, identifier);
NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, index));
CFRelease(phones);

通常,这些值与检索的ABMultiValueIdentifier值匹配,但如果编辑了联系人(特别是如果删除了较早的电话号码之一),则使用in将返回错误的记录。CFIndexABMultiValueGetIndexForIdentifierABMultiValueIdentifierABMultiValueCopyValueAtIndex

于 2014-09-10T15:49:49.063 回答
0

你可以很容易地像这样迭代。尝试 NSLoging 该代码以确保其正常工作。我认为您的“选择”逻辑中有一些错误。

ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);

for (CFIndex i=0; i < ABMultiValueGetCount(phones); i++) 
{
   NSString* phoneLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
   NSString* phoneNumber = ABMultiValueCopyValueAtIndex(phones, i);

   //release variables since you were using COPY !!!
   CFRelease(phoneNumber);
   CFRelease(phoneLabel);
}

CFRelease(phones);
于 2013-11-22T09:59:32.397 回答
0

最终以这种方式实现它:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
   if (property == kABPersonPhoneProperty)
    {
        ABMultiValueRef phones = ABRecordCopyValue(person, property);
        CFStringRef phoneNumber = ABMultiValueCopyValueAtIndex(phones, identifier);
        NSLog(@"%@", phoneNumber);
        NSMutableString *tmp = [NSMutableString stringWithFormat:@"%@", (__bridge_transfer NSString *)phoneNumber];
        NSString *strippedPhoneNumber = [tmp stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"()-"];
        strippedPhoneNumber = [[strippedPhoneNumber componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];
        NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        [peoplePicker dismissViewControllerAnimated:YES completion:nil];
        return NO;
}
于 2013-11-26T09:05:48.420 回答