简体   繁体   中英

ABMultiValueIdentifier is always 0 (Zero) ABPeoplePicker

I am trying to give the user the possibility to select a phone number from their Contacts and then display the chosen number in a UITextField.

The problem is that the returned ABMultiValueIdentifier from shouldContinueAfterSelectingPerson is always 0 no matter which number you select on a contact.

This is my code:

- (IBAction)btnChooseContactClicked:(id)sender {
    [[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];    

    [picker setDisplayedProperties: [NSArray arrayWithObjects: [NSNumber numberWithInt: kABPersonPhoneProperty], nil]];
    picker.peoplePickerDelegate = self;   

    [self presentModalViewController:picker animated:YES];
}


- (void)peoplePickerNavigationControllerDidCancel:
(ABPeoplePickerNavigationController *)peoplePicker
{
    [self dismissModalViewControllerAnimated:YES];
}


- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person {

    return YES;
}

- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person
                                property:(ABPropertyID)property
                              identifier:(ABMultiValueIdentifier)identifier //Always = 0
{
    [self displayPerson:person property:property identifier:identifier];
    [self dismissModalViewControllerAnimated:YES];
    return NO;
}

- (void)displayPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSLog(name);

    if (property == kABPersonPhoneProperty) {
        ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
        for(CFIndex i = 0; i < ABMultiValueGetCount(multiPhones); i++) {
            if(identifier == ABMultiValueGetIdentifierAtIndex (multiPhones, i)) {
                CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
                CFRelease(multiPhones);
                NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;
                CFRelease(phoneNumberRef);
                self.txtTelNo.text = phoneNumber;
            }
        }
    }

}

It feels like I am doing everything right and I have followed countless others and nothing seems to work. What could be the issue?

In iOS8 there is the option to use the ABPeoplePickerViewController without first authenticating. If you are doing this, then you are only allowed to access the data for the value chosen by the user - all other id's are set to invalid.

Try authenticating first before opening the ABPeoplePickerViewController and check if you are receiving the correct data.

I had the same issue when trying to retrieve the thumbnail image for the person being picked - without authenticating this was always empty.

It turnes out that it was my test-user in my phone that was the problem. This user had 2 phone numers, both of which were set to "Mobile" type. It seems like the picker cant see a difference between them if this is the case. Can anyone confirm this or am I talking out of my ass here?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM