Andre Cytryn
Andre Cytryn

Reputation: 2705

How to edit a record on ABAddressBook

I want to be able to select a record from the ABPeoplePickerNavigationController and then add an email to that contact but from my view controller.

I tried to search on the ABAddressBook class reference to see what I can do but had no insights =/

I am stuck here with the person reference, anyone know a way to proceed?

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person

I am having an EXC_BAD_ACCESS when using ABRecordSetValue(person, kABPersonEmailProperty, (__bridge CFStringRef)@"[email protected]", &error); but with no log error.

UPDATE

ok, I managed to save it inside the peoplePicker delegate like this:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {

    ...

    self.addressBook = peoplePicker.addressBook;
    self.selectedPerson = person;

    // adding to native contacts
    CFErrorRef error = nil;
    ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);
    bool didAddEmail = ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(email), kABOtherLabel, NULL);
    if(didAddEmail){
        ABRecordSetValue(self.selectedPerson, kABPersonEmailProperty, emailMultiValue, nil);
        NSLog(@"Email saved......");
    } else {
        NSLog(@"Error adding email: %@", error);
    }
    ABAddressBookSave(self.addressBook, NULL);

    ...
}

The weird thing is that if I try to do that outside this method, like after the users input on a textField, I get an error.

Then check my logs while inside and the outside the peoplePickerNavigationController method above:

(lldb) po self.selectedPerson
(ABRecordRef) $1 = 0x0a1838f0 <CPRecord: 0xa1838f0 ABPerson>
(lldb) po self.selectedPerson
(ABRecordRef) $4 = 0x0a1838f0 [no Objective-C description available]

The first log seems to be alright, because it was done inside the delegates method, but the second is weird, I mean the memory reference is there but not the content... The second one was logged after the peoplePicker being dismissed...

why did that happened?

my .h file:

@property (nonatomic) ABAddressBookRef addressBook;
@property (nonatomic) ABRecordRef selectedPerson;

Upvotes: 1

Views: 952

Answers (1)

Andre Cytryn
Andre Cytryn

Reputation: 2705

My solution was to save the person ID and then access the AddressBook again and use: ABAddressBookGetPersonWithRecordID(addressBook, self.selectedPersonID)

And create the addressBook again.

ABAddressBookRef addressBook = ABAddressBookCreate();

Upvotes: 1

Related Questions