0

I want to delete a specific phone number from an address book contact.

It "seems" to work, because it is returning true, but it doesn't.

Please, could anyone help me out here? This would be awesome!

ABAddressBookRef ab = ABAddressBookCreate();
 ABRecordRef record = ABAddressBookGetPersonWithRecordID(ab,[myID intValue]);
     NSError *error = NULL;

ABMultiValueRef phoneNumbers = ABRecordCopyValue(record,kABPersonPhoneProperty);

for(CFIndex i=0; i < ABMultiValueGetCount(phoneNumbers); i++){
      NSString *phoneNumber = (NSString *)ABMultiValueCopyValueAtIndex(phoneNumbers,i);
      //ckDebug(@"phoneNumber = %@", phoneNumber);

      if ([[oDict objectForKey:@"value"] isEqualToString:phoneNumber]) {

              BOOL didRemove = ABMultiValueRemoveValueAndLabelAtIndex(ABMultiValueCreateMutableCopy(phoneNumbers),i);
              ckDebug(@"didRemove = %@\n", (didRemove ? @"TRUE" : @"FALSE"));

              //and save it!
              BOOL didSave = ABAddressBookSave(ab, (CFErrorRef *) error);

              ckDebug(@"didSave = %@\n", (didSave ? @"TRUE" : @"FALSE"));
              if (error) {
                  ckDebug(@"ABAddressBookSaveError = %@", error);
              }
      }
      [phoneNumber release];
}
CFRelease(ab);
Bart
  • 19,692
  • 7
  • 68
  • 77
code-K
  • 35
  • 5

2 Answers2

2

So the Solution is:

Creating the MutableCopy of the MultiValueRef, then delete the value from there and set the copy back to the record and save...

like this:

ABAddressBookRef ab = ABAddressBookCreate();
    ABRecordRef record = ABAddressBookGetPersonWithRecordID(ab,[myID intValue]);
    NSError *error = NULL;

    ABMultiValueRef phoneNumbers = ABRecordCopyValue(record,kABPersonPhoneProperty);
    ABMutableMultiValueRef phoneNumberMV = ABMultiValueCreateMutableCopy(phoneNumbers);

    for(CFIndex i=0; i < ABMultiValueGetCount(phoneNumberMV); i++){
        NSString *phoneNumber = (NSString *)ABMultiValueCopyValueAtIndex(phoneNumberMV,i);
        //ckDebug(@"phoneNumber = %@", phoneNumber);

        if ([[oDict objectForKey:@"value"] isEqualToString:phoneNumber]) {
            //now delete it!!! ;-)
            /*
            ckDebug(@"phoneNumbers = %@",phoneNumbers);
            ckDebug(@"index = %d", i);
            */
            BOOL didRemove = ABMultiValueRemoveValueAndLabelAtIndex(phoneNumberMV,i);
            ckDebug(@"didRemove = %@\n", (didRemove ? @"TRUE" : @"FALSE"));

            BOOL didSet = ABRecordSetValue(record, kABPersonPhoneProperty, phoneNumberMV, nil);
            ckDebug(@"didSet = %@\n", (didSet ? @"TRUE" : @"FALSE"));

            //and save it!
            BOOL didSave = ABAddressBookSave(ab, (CFErrorRef *) error);

            ckDebug(@"didSave = %@\n", (didSave ? @"TRUE" : @"FALSE"));
            if (error) {
                ckDebug(@"ABAddressBookSaveError = %@", error);
            }
        }
        [phoneNumber release];
    }
    CFRelease(ab);
code-K
  • 35
  • 5
1

You remove the phone number from a copy of the phone numbers field, but never update the record with the modified list. You need to call ABRecordSetValue before you call ABAddressBookSave.

idz
  • 12,825
  • 1
  • 29
  • 40