0

Help. I want to do a simple application to update ALL Phone Labels to another specific. I need to change all (work, home, iphone) to "Sync".

I tried to do this, but didn't work. Somebody can help me, please?

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
//ABRecordRef record = ABAddressBookGetPersonWithRecordID(addressBookRef, ABRecordGetRecordID(record));

//NSString *addressBookNum;
NSString *phoneNumber;
NSString *tagLabel;


if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
    ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {

    });
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {

    CFErrorRef *error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

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

        ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );

        ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

        [[UIDevice currentDevice] name];

        for (CFIndex k = 0; i < ABMultiValueGetCount(phoneNumbers); k++) {

            phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, k);
            tagLabel = (__bridge NSString*) ABMultiValueCopyLabelAtIndex(phoneNumbers, k);

            CFStringRef newTagLabel = (CFStringRef)@"Sync";

            ABMultiValueReplaceLabelAtIndex(phoneNumbers, newTagLabel, k);

            ABAddressBookSave(person, (CFErrorRef *) error);

            }

        }



}
else {
    // Send an alert telling user to change privacy setting in settings app
}

1 Answers1

0

The problem is your statement

ABAddressBookSave(person, (CFErrorRef *) error);  

The function ABAddressBookSave is defined as

bool ABAddressBookSave (
   ABAddressBookRef addressBook,
   CFErrorRef *error
);  

So the 1st argument is a reference to the address book, not to a person record.
You had to save the address book after you have done all of your changes.

EDIT (due to your comment below):

The error message means that you are trying to apply function ABMultiValueReplaceLabelAtIndex(...) to a non-mutable record, as phoneNumbers is. It is non-mutable even if you assign it to a reference of type ABMutableMultiValueRef! You had to create first a mutable copy using something like

ABMutableMultiValueRef mutablePhoneNumbers = ABMultiValueCreateMutableCopy(phoneNumbers);

Only than can the label be changed.
Independently of this, my first argument still applies, and you had to save the changes of your address book at the end.

Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
  • Hello Reinhard. Thanks for the quick answer, but I get an error in the line above: ABMultiValueReplaceLabelAtIndex(phoneNumbers, newTagLabel, k); I got this error: Assertion failed: (((ABCMultiValue *)multiValue)->flags.isMutable), function ABMultiValueReplaceLabelAtIndex, file /SourceCache/AddressBook_Sim/AddressBook-10898.7/ABMultiValue.c, line 124. – Alessandro Frizzera May 29 '14 at 23:46