Pushing ABPersonViewController to ABPeoplePickerNavigationController on iOS8

1.5k Views Asked by At

Presenting the people picker

ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
peoplePicker.allowsActions = YES;
peoplePicker.allowsEditing = NO;
peoplePicker.peoplePickerDelegate = self;
[self presentViewController:peoplePicker animated:YES completion:nil];

Implementing the ABPeoplePickerNavigationControllerDelegate in iOS 7

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
  ABPersonViewController *personViewController = [[ABPersonViewController alloc] init];
  personViewController.displayedPerson = person;
  [peoplePicker pushViewController:personViewController animated:YES];
  return NO;
}

So far so good. The person view controller is presented as expected. iOS7 method returns a value - one could return NO in order to make sure the people picker remains open. In iOS8 the above delegate method was deprecated and new method must be implemented:

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person {
  ABPersonViewController *personViewController = [[ABPersonViewController alloc] init];
  personViewController.displayedPerson = person;
  [peoplePicker pushViewController:personViewController animated:YES];
}

The person view controller is pushed to the people picker but after a fraction of a second the people picker is dismissed (together with the person view controller).

Is there a way to prevent the people picker from dismissing on iOS8? Any other suggestions?

3

There are 3 best solutions below

0
Arsad Alam On
-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person
{ 
    [self dismissViewControllerAnimated:NO completion:^{
      ABPersonViewController *personController = [[ABPersonViewController alloc] init];

      [personController setDisplayedPerson:person];
      [personController setPersonViewDelegate:self];
      [personController setAllowsEditing:NO];
      personController.displayedProperties = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty], nil];

      [self.navigationController pushViewController:personController animated:YES];
    }];
}
4
Hristo Atanasov On

How about using presentViewController instead pushViewController ... in Xcode 6 it says that pushViewController is deprecated and I see you're trying to implement it in iOS8 so give it a try ... Something like this line:

[peoplePicker presentViewController: personViewController animated: YES completion: nil];
0
Wei Zhang On

In iOS8, you will need to add code as below when you initialise the ABPeoplePickerNavigationController, otherwise the peoplePickerNavigationController will dismiss right away after you select a contact.

if(IOS8_OR_LATER){
    peoplePicker.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false]; }