0

我想从通讯录联系人中删除特定的电话号码。

它“似乎”起作用,因为它返回 true,但事实并非如此。

拜托,有人可以帮我吗?这太棒了!

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);
4

2 回答 2

2

所以解决方案是:

创建 MultiValueRef 的 MutableCopy,然后从那里删除值并将副本设置回记录并保存...

像这样:

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);
于 2012-05-24T08:46:25.953 回答
1

您从电话号码字段的副本中删除电话号码,但从不使用修改后的列表更新记录。你需要先打电话ABRecordSetValue,然后再打电话ABAddressBookSave

于 2012-05-23T23:15:22.687 回答