简体   繁体   English

ABMultiValueIdentifier始终为0(零)ABPeoplePicker

[英]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. 我试图让用户可以从他们的联系人中选择一个电话号码,然后在UITextField中显示所选的号码。

The problem is that the returned ABMultiValueIdentifier from shouldContinueAfterSelectingPerson is always 0 no matter which number you select on a contact. 问题是,无论您在联系人上选择哪个号码,从shouldContinueAfterSelectingPerson返回的ABMultiValueIdentifier始终为0。

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. 在iOS8中,无需先进行身份验证即可使用ABPeoplePickerViewController。 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. 如果这样做,则只允许访问用户选择的值的数据-所有其他ID均设置为无效。

Try authenticating first before opening the ABPeoplePickerViewController and check if you are receiving the correct data. 在打开ABPeoplePickerViewController之前,请先尝试进行身份验证,然后检查是否接收到正确的数据。

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. 该用户有2个电话号码,两个电话号码均设置为“移动”类型。 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? 任何人都可以确认这一点,或者我是在这里说话呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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