5

In ios8, I would like to access contact properties if he has more than one numberphone but I don't know how to do it in iOS8.

Here is my code in iOS7 :

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

    //If person has just one phone number
    ABMultiValueRef phonesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
    if(ABMultiValueGetCount(phonesRef) == 1){

        CPIContact* contact = [self getCPIContactFromPerson:person andPhoneIndex:0];
        [self addContact:contact];

        // Dismiss the address book view controller.
        [_addressBookController dismissViewControllerAnimated:YES completion:nil];
        return NO;

    }else if(ABMultiValueGetCount(phonesRef) == 0){

        [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Common_information",nil) message:NSLocalizedString(@"EditCallSMS_noNumber", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"Common_ok",nil) otherButtonTitles:nil] show];

        return NO;
    }
    else{
        return YES;
    }

}

I know I have to use the method didSelectPerson from iOS8 but I don't know how to tell the app that it can continue after selecting a person like in iOS7.

I read about predicateForSelectionOfPerson on apple documentation but I don't understand how to use it.

https://developer.apple.com/library/ios/documentation/AddressBookUI/Reference/ABPeoplePickerNavigationController_Class/index.html#//apple_ref/occ/instp/ABPeoplePickerNavigationController/predicateForSelectionOfProperty

Thank you in advance for your help.

Leep
  • 447
  • 1
  • 3
  • 11

1 Answers1

10

Add this where you instantiate the people picker:

if ([peoplePicker respondsToSelector:@selector(setPredicateForSelectionOfPerson:)])
{
     peoplePicker.predicateForSelectionOfPerson = [NSPredicate predicateWithFormat:@"%K.@count > 1", ABPersonPhoneNumbersProperty];
}

This will only let you choose contacts with 2 or more phone numbers. For other contacts, you will be shown the contact details.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    Thanks ! That is what I needed ! It was the contrary ("%K.@count < 2") but now I understand how to use predicates with the people picker. – Leep Oct 06 '14 at 12:38
  • You may also need: if ([picker respondsToSelector: @selector(setPredicateForEnablingPerson:)]) { picker.predicateForEnablingPerson = [NSPredicate predicateWithFormat:@"emailAddresses.@count > 0"]; } Without that, my app would not select a contact at all – Peter Johnson Dec 17 '14 at 13:35
  • @Leep When I add `("%K.@count < 2") `, All the contacts with less than 2 phone numbers are grayed out. – user1324887 Jul 19 '16 at 06:53