49

Since I have updated XCode (6.0, 6A313) and my iOS (8.0, 12A365) on the iPhone to gm seeds, the ABPeoplePickerNavigationController code doesn't work like before.

  • iOS 7.1.2: If someone want to import a contact, the address book opens and you see the full list of contacts, after picking one, it opens detail view of an contact and than you can add the contact with a click of the phone number you want to import.

  • iOS 8.0: its everything similar but if you click on number of an contact it dial the phone number instead of importing it..

Code:

#pragma mark - AddressBook Delegate Methods

-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
    return YES;
}


-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{

    // Get the first and the last name. Actually, copy their values using the person object and the appropriate
    // properties into two string variables equivalently.
    // Watch out the ABRecordCopyValue method below. Also, notice that we cast to NSString *.
    NSString *firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

    // Compose the full name.
    NSString *fullName = @"";
    // Before adding the first and the last name in the fullName string make sure that these values are filled in.
    if (firstName != nil) {
        fullName = [fullName stringByAppendingString:firstName];
    }
    if (lastName != nil) {
        fullName = [fullName stringByAppendingString:@" "];
        fullName = [fullName stringByAppendingString:lastName];
    }


    // Get the multivalue number property.
    CFTypeRef multivalue = ABRecordCopyValue(person, property);

    // Get the index of the selected number. Remember that the number multi-value property is being returned as an array.
    CFIndex index = ABMultiValueGetIndexForIdentifier(multivalue, identifier);

    // Copy the number value into a string.
    NSString *number = (__bridge NSString *)ABMultiValueCopyValueAtIndex(multivalue, index);

    nameTextField.text = fullName;
    numberTextField.text = number;

    // Dismiss the contacts view controller.
    [_addressBookController dismissViewControllerAnimated:YES completion:nil];

    return NO;
}


// Implement this delegate method to make the Cancel button of the Address Book working.
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
    [_addressBookController dismissViewControllerAnimated:YES completion:nil];
}

couldn't find any answer in iOS developer library of apple. have somebody else a solution for it?

manntobias
  • 501
  • 1
  • 4
  • 10

3 Answers3

79

iOS 8 requires a new delegate method be implemented for this:

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

Keep the old delegate method in place to support iOS 7 or earlier. What I do in my app is call the iOS 7 delegate method from the iOS 8 delegate method:

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
    [self peoplePickerNavigationController:peoplePicker shouldContinueAfterSelectingPerson:person property:property identifier:identifier];
}

If this delegate method isn't implemented in iOS 8, tapping the value causes the action. When implemented, the delegate is called instead with the selected value.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • This isn't working for me on iOS 8.0.1. Are there other delegate methods I need to implement? My delegate isn't being hit at all, except for the cancel method. – Alexander Sep 24 '14 at 18:19
  • @AlexanderCollins It works just fine for me with 8.0.1. Are you sure your delegate is set? – rmaddy Sep 24 '14 at 18:22
  • Yep, my cancel delegate method is being hit correctly. Selecting a contact's phone number just calls that contact instead of hitting my didSelect: delegate method. – Alexander Sep 24 '14 at 18:32
  • @AlexanderCollins You should post your own question with the needed details. – rmaddy Sep 24 '14 at 18:32
  • @Alexander and all who try this under iOS 7 The delegate method will not work under the new iOS 8 delegate method hood, it will not call it at all of course, so you need to leave the iOS 7 delegate method (shouldContinueAfterSelectingPerson:) intact – BootMaker Nov 13 '14 at 10:42
  • @Alexander What can be done to resolve this issue in iOS 8.0.1 ? – user2955351 Nov 26 '14 at 06:34
13

See also the delegate method, new with iOS8:

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person;
{
    [self selectedPerson:person];
}

That's what I wanted in my case.

Chris Prince
  • 7,288
  • 2
  • 48
  • 66
1

This worked for me on both iOS 8 and iOS 7 and lower.

Note I am using this didSelectPerson:(ABRecordRef)person instead.

//Needed for iOS 8
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
    NSLog(@"Went here 1 ...");

    [self peoplePickerNavigationController:peoplePicker shouldContinueAfterSelectingPerson:person];
}


//needed for iOS 7 and lower
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person 
{

    NSLog(@"Went here 2 ...");

    //add your logic here

}
Sam B
  • 27,273
  • 15
  • 84
  • 121