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

元气小坏坏 提交于 2019-12-23 19:28:49

问题


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:


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!



来源:https://stackoverflow.com/questions/16258304/how-to-edit-a-phone-number-values-programmatically-from-address-book-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!