3
votes

I'm trying to replace an specific phone number for an specific contact programmatically in iOS, taking the contacts form address book.

I don't know why I can't save the new phone number and refresh the address book to show the change.

I'm doing this:

+(BOOL) changeContactPhoneNumber:(NSString *) phoneSought
              forThis:(NSString *) newPhoneNumber{

ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef contactSelected;
CFStringRef mobileLabelNumber;
CFErrorRef error = nil;

// Do whatever you want here.
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

for (int i = 0; i < nPeople; i++)
{

    ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);

    ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(ref, kABPersonPhoneProperty);
    NSString* mobilePhoneNumber=@"";


    if (ABMultiValueGetCount(phones) > 0) {
        for (int i=0; i < ABMultiValueGetCount(phones); i++) {
            [mobilePhoneNumber release];
            mobilePhoneNumber = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);

            if([mobilePhoneNumber isEqualToString:phoneSought]){
                contactSelected = ref;
                mobileLabelNumber = ABMultiValueCopyLabelAtIndex(phones, i);
            }
        }
    }
}

ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABPersonPhoneProperty);
bool didAddPhone = ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,(__bridge CFTypeRef)newPhoneNumber,mobileLabelNumber, NULL);


if(didAddPhone){
    ABRecordSetValue(ABAddressBookGetPersonWithRecordID(addressBook, contactSelected),
                     kABPersonPhoneProperty,
                     phoneNumberMultiValue,
                     nil);

    bool bSuccess = ABAddressBookSave(addressBook, &error);
    if (!bSuccess) {
        NSLog(@"Could not save to address book: %@", error);
    } else {
        return YES;
    }

} else {
    NSLog(@"Error editing phone number: %@", error);
    error = nil;
}

return NO;
}
1
May i know which IOS version you are using?Deepesh Gairola
right now i'm using from iOs 4.3 to the last version (6.1.3)... I have another method before where i ask for permissions if the user has iOs 6bubudrc
You make no mention of what is actually happening with this code. How far does it get? What happens?rmaddy
the problem is that the phone number never updated. I mean, the method does'n return me an error, but if I back to the address book and look in the modified contact, the phone number is the same, never changedbubudrc

1 Answers

3
votes

You should debug your code and try to figure out whether the format of the phone numbers you are providing to the method are matching or not.

For e.g. when i am logging my contact list phone numbers these are results

Number...555-478-7672
Number...(408) 439-5270
Number...(408) 555-3514
Number...888-555-5512
Number...888-555-1212
Number...555-522-8243
Number...(555) 766-4823
Number...(707) 555-1854
Number...555-610-6679

And i was comparing these number against unformatted number string.

Secondly

ABRecordSetValue(ABAddressBookGetPersonWithRecordID(addressBook, contactSelected),
                 kABPersonPhoneProperty,
                 phoneNumberMultiValue,
                 nil);

Whose actual declaration is

ABRecordSetValue(ABRecordRef record, ABPropertyID property, CFTypeRef value, CFErrorRef* error); 

Although ABAddressBookGetPersonWithRecordID returns a ABRecordRef but you already have ABRecordRef contactSelected; so in my view you should use

ABRecordSetValue(contactSelected,kABPersonPhoneProperty,phoneNumberMultiValue,nil);

Please correct me if i am wrong or have misunderstood your code!