iPhone ABPeoplePickerNavigationController - How to select two single entries of two different multivalue properties of a person from Addressbook
Asked Answered
F

4

11

I'm near desperation as I search for a solution for weeks now.

The Problem is simple:

  • Via the ABPeoplePickerNavigationController (as a ModalView), a person should be selected.
  • Then only (e.g.) the Mail addresses should be shown and the user should select one.
  • After selection of a Mail address just the (e.g.) phone numbers should be shown and the user should select one.

The solution until the third aspect is well-known:

- (IBAction)importFromAddressBook 
{
    ABPeoplePickerNavigationController* picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker 
{
    [self dismissModalViewControllerAnimated:YES];
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker 
      shouldContinueAfterSelectingPerson:(ABRecordRef)person 
{
    [peoplePicker setDisplayedProperties:[NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonEmailProperty]]];
    return YES;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person
                                property:(ABPropertyID)property
                              identifier:(ABMultiValueIdentifier)identifier 
{
    //===PROBLEM=== Now I do have a mail address and I want to have a phone number right afterwards.

    //Something like this would be straightforward, but the view does not change this way:
    [peoplePicker setDisplayedProperties:[NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]]];
    //Here misses the important code.


    //And only when I also got a phone number through this or a similar method I want to call:
    [peoplePicker dismissModalViewControllerAnimated:YES];

    //I do not want to start default behaviour with the mailaddress and/or phone number:
    return NO;
}

The right approach seems to push a similar peoplePicker View on the NavigationController of the ModalView, but I don't know how.

If anyone had any idea it would be great!

If you want to see such a behavior in action you can have a look at the Amazon app: If you go through the first steps of an order you can select a shipping address exactly this way: From Contact List -> Select a Person -> Select an Address -> Select a Telephone number. There, everything (seems to) take place in the modal view with just a navigation hierarchy with one more level than in the standard code shown above.

Farmstead answered 24/2, 2010 at 23:58 Comment(0)
E
15

I guess this might be what you want:

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

    ABPersonViewController *controller = [[ABPersonViewController alloc] init];
    controller.displayedPerson = person;
    controller.displayedProperties = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]];
    controller.personViewDelegate = self;
    [peoplePicker pushViewController:controller animated:YES];
    [controller release];
    return NO;
}

- (BOOL)personViewController:(ABPersonViewController *)personViewController 
shouldPerformDefaultActionForPerson:(ABRecordRef)person 
                    property:(ABPropertyID)property
                  identifier:(ABMultiValueIdentifier)identifierForValue
{
    ABMutableMultiValueRef multi = ABRecordCopyValue(person, property);
    CFStringRef phone = ABMultiValueCopyValueAtIndex(multi, identifierForValue);
    NSLog(@"phone %@", (NSString *)phone);
    CFRelease(phone);

    ABPeoplePickerNavigationController *peoplePicker = (ABPeoplePickerNavigationController *)personViewController.navigationController;
    [peoplePicker dismissModalViewControllerAnimated:YES];
    return NO;
}

The idea is, to create another ABPersonViewController instance, and let your people picker to push it, since ABPeoplePickerNavigationController is a subclass of NSPeoplePickerNavigationController.

Elum answered 16/3, 2010 at 20:54 Comment(2)
I want to say sorry to you, too. It was not my intention not to react. This was exactly what I searched for!Demonetize
There is an error in the code -- you should NOT use the identifier as the index in the line ABMultiValueCopyValueAtIndex(multi, identifierForValue). Convert identiefier to a property index through ABMultiValueGetIndexForIdentifierEcg
P
1

In my iPhone app Pastie, I took a different approach. alt text
(source: manicwave.com)

I use the peoplePicker to select the person and then open a contact (person) editor.

This is just a simple view:

Contact Name Phone Number > defaults to first phone number email Address > defaults to first email address

Each of phone number and email address bring up another view showing the list of phones or email addresses, with a check mark next to the currently selected one.

I use this view for initial setup of a contact as well as subsequent editing.

Pennant answered 28/2, 2010 at 4:45 Comment(1)
I'm so sorry I didn't answer yet! Your comment guided me to the solution which was almost exactly like the one of zonble. Thank you very much!!Demonetize
E
1

In the suggested answer there is a release missing

CFRelease(multi);

Without this release a leak will occur. Or at least according to the Build and Analyze in Xcode....

Eadwina answered 22/5, 2010 at 7:59 Comment(0)
D
1

The following method should return NO:

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

This will allow your next method be called (peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier:).

Debouch answered 20/7, 2010 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.