如果联系人的号码超过 1 phone 个,则显示详细的联系人名片

Display detailed contact card if contact has more than 1 phone number

如果联系人只有 1 个 phone 号码,我想 select 它。如果它有超过 1 phone 个号码,那么我想显示详细的名片。

我正在使用下面的方法,效果很好。

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person {

    if (person != nil) {
        ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
        if (ABMultiValueGetCount(phoneNumbers) == 1) {
            //Do stuff to select phone number
        }
    }
}

然后,我实现了这个 Utility 方法来显示详细的联系人名片:

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {

    NSString* phone = nil;
    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
    if (ABMultiValueGetCount(phoneNumbers) > 0) {
        CFIndex index = ABMultiValueGetIndexForIdentifier(phoneNumbers, identifier);
        phone = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, index);
    }
}

但是这个方法从来没有被调用过? 如果我注释掉第一个方法,则会调用第二个方法。我如何调用两者,或者有其他方法可以实现吗?

解决方案是在开始 peoplePicker 之前添加一个 predicate 在这种情况下,它是:

-(void)getContacts {
    ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
    peoplePicker.peoplePickerDelegate = self;
    if ([peoplePicker respondsToSelector:@selector(setPredicateForSelectionOfPerson:)])
    {
        // The people picker will select a person that has exactly one phone number and call peoplePickerNavigationController:didSelectPerson:,
        // otherwise the people picker will present an ABPersonViewController for the user to pick one of the Phone Numbers.
        peoplePicker.predicateForSelectionOfPerson = [NSPredicate predicateWithFormat:@"phoneNumbers.@count = 1"];

    }
    [self presentViewController:peoplePicker animated:NO completion:nil];
}

与电子邮件地址类似,将:@"phoneNumbers.@count = 1" 替换为 @"emailAddresses.@count = 1"