peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier:

时间:2013-11-22 09:53:53

标签: ios objective-c abaddressbook abpeoplepickerview

我正在尝试使用

选择手机号码
ABMultiValueRef phones = ABRecordCopyValue(person, property);
CFStringRef phoneNumber = ABMultiValueCopyValueAtIndex(phones, identifier);

我与几部手机有联系(全都标有'手机')。当我选择第一个时,phoneNumber给我第一个,但如果我选择任何一个,phoneNumber给我以前的数字:

联系人:     杰伊杰姆斯     手机+1111111111     手机+2222222222     手机+3333333333

点击第一个,phoneNumber = +1111111111

点击第二个,phoneNumber = +1111111111

点击第三个,phoneNumber = +2222222222

4 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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);

答案 2 :(得分: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;
}

答案 3 :(得分:0)

您不应该使用identifier作为ABMultiValueCopyValueAtIndex中的索引。您应该致电ABMultiValueGetIndexForIdentifierABMultiValueIdentifier标识转换为CFIndex索引:

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

通常,ABMultiValueIdentifier值与CFIndex检索到的ABMultiValueGetIndexForIdentifier值相匹配,但如果联系人已被编辑(特别是如果删除了之前的某个电话号码),则使用{ ABMultiValueIdentifier中的{1}}将返回错误的记录。