如果联系人有超过1个电话号码,则显示详细的联系人卡片

时间:2016-07-20 03:22:17

标签: ios xcode ios8 xcode8 abpeoplepickerview

如果联系人只有一个电话号码,我想选择它。如果它有超过1个电话号码,那么我想显示详细的联系卡。

我正在使用以下方法,它工作正常。

- (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
        }
    }
}

然后,我实施此实用程序方法以显示详细联系人卡片:

- (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);
    }
}

但这个方法永远不会被调用? 如果我注释掉第一个方法,那么第二个方法就会被调用。我怎么能同时打电话,还是有另一种方式来实现这个目标?

1 个答案:

答案 0 :(得分:0)

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

-(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];
}

同样,对于emailAdresses,请将@"phoneNumbers.@count = 1"替换为@"emailAddresses.@count = 1"