简体   繁体   中英

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

If a contact has only 1 phone number, I want to select it. If it has more than 1 phone number, then I want to display the detailed contact card.

I am using the below method, which works fine.

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

Then, I implement this Utility method to display Detailed Contact card:

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

but this method never gets called? If I comment out the first method, then the second method gets called. How can I call both, or is there another way to achieve this?

The solution is to do this is to add a predicate before starting the peoplePicker in this case it would be:

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

similarly in case of emailAdresses, replace : @"phoneNumbers.@count = 1" with @"emailAddresses.@count = 1" .

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