Jeff Hay
Jeff Hay

Reputation: 2645

Custom addressbook for ABPeoplePickerNavigationController on iPhone

I am trying to create a custom addressbook on the iPhone by adding new records to the device address book (basically, merging a private contact list with the device AB). I then want to let the user select a contact, which I thought I could do with the .addressbook property of ABPeoplePickerNavigationController:

ABAddressBookRef contacts = ABAddressBookCreate();

for (PrivateUserType *user in rosterItems)
{
  CFErrorRef err = NULL;
  ABRecordRef ref = ABPersonCreate();
  ABRecordSetValue(ref, kABPersonLastNameProperty, (CFStringRef)user.lastName, &err);

  if (err != NULL)
  {
    NSString *errorStr = [(NSString *)CFErrorCopyDescription(err) autorelease];
    NSLog(@"Can not set name: %@", errorStr);
  }

  ...

  ABAddressBookAddRecord(contacts, ref, &err);
  if (err != NULL)
  {
    NSString *errorStr = [(NSString *)CFErrorCopyDescription(err) autorelease];
    NSLog(@"Error adding XMPP roster user to addressbook: %@", errorStr);
  }
}

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.addressBook = contacts;
[viewController presentModalViewController:picker animated:YES];
[picker release];

The entries are successfully added to the addressbook (which I can verify if I do an ABAddressBookSave() after the loop to add everything in). However, the entries are not shown by picker (unless I save contacts, which of course saves all changes into the device addressbook).

Is there a way to do this? This is on iOS 4.3.1.

Thanks!

Upvotes: 1

Views: 3822

Answers (2)

Sergnsk
Sergnsk

Reputation: 3303

As Toro said, you need save any changes to database first. ABPeoplePickerNavigationController not intended to work with uncommit data or with custom data.

You can see rather fresh topic on Apple Dev Forums https://devforums.apple.com/message/370070#370070 where similar question was asked. And guy from Apple says that "I can't see any way to do this." But he suggest filling a bug for this to implement this feature in future release of iOS.

All you can do now is code your own UI for this AddressBook from the ground :(

updated: he-he. I found your question on App Dev Forum too )))

Upvotes: 2

AechoLiu
AechoLiu

Reputation: 18428

You need to save any changes to database by ABAddressBookSave() method.

The apple doc link.

Upvotes: 0

Related Questions