iphoneobjective-ccocoa-touchabaddressbookabrecordref

Contact being removed from AddressBook after being Selected


I'm working on some integration between my application and the iPhone's AddressBook. Here is the flow of my program.

  1. User wants to import a contact
  2. App Presents "ABPeoplePickerNavigationController" to the User.
  3. User selects the contact they want:
  4. Delegate Method is called:

Here is the code:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker 
      shouldContinueAfterSelectingPerson:(ABRecordRef)person {
    self.selectedContact = person;
    [self showNewContactViewThroughController:peoplePicker withRecord:person];
    NSLog(@"should continue after selecting");
    return NO;
}

5: In - (void)showNewContactViewThroughController:withRecord: We create the AbNewPersonViewController.

`ABNewPersonViewController *newController = [[ABNewPersonViewController alloc] init];`

`newController.displayedPerson = person;`

`newController.newPersonViewDelegate = self;`

And then push it.

6: User hits "Cancel" to quit the ABNewPersonViewController view.
7: Check the Contacts app, the contact they selected in step 3 is gone. Poof, gone, deleted, removed.

In an attempt to fix this issue, I save the ABRecordRef (to the instance variable "selectedContact"). Then, in - (void)newPersonViewController:didCompleteWithNewPerson: I have:

if (person) {
//do stuff with the person
else {
        ///
        /// This means they canceled, so we need to save the old person back
        ///
        if (self.selectedContact) {
            ABAddressBookRef addressBook = ABAddressBookCreate();
            CFErrorRef error = NULL;
            ABAddressBookAddRecord(addressBook, self.selectedContact, &error);
            if (error != NULL) {
                NSLog(@"Error Adding Record: %@", error);
            }
            ABAddressBookSave(addressBook, &error);
            if (error != NULL) {
                NSLog(@"AccountDetailTableViewController.newPersonViewController() The old contact was not saved successfully. %@", error);
            }
            self.selectedContact = nil;
        }
    }

However, this doesn't seem to do anything. The code is executed, but the "Old Contact", self.selectedContact, is not saved to the AddressBook. So, my contacts are still disappearing. What am I doing wrong? It appears as though if you hit Cancel in the ABNewPersonViewController, it "removes" the data it was given from the Address Book? So the person I give it then dies? Any help would be appreciated!


Solution

  • I solved this problem by NOT using the ABNewPersonViewController, but rather by using the ABPersonViewController after the user selected the person they want to use. The information is no longer being deleted.