简体   繁体   中英

how to edit a phone number values programmatically from address book ios

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;
}

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 eg 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM