2

My code is this, I pass the array to CFArrayRef then delete the Contacts but code is not working:

 CFArrayRef arrayRef = (__bridge CFArrayRef)multipleSeleted;
 ABAddressBookRef addressbook = ABAddressBookCreate();
 if (arrayRef != NULL)     {
      int count = CFArrayGetCount(arrayRef);
      for (int i = 0; i < count; ++i){
           ABRecordRef contact = CFArrayGetValueAtIndex(arrayRef, i);
           ABAddressBookRemoveRecord(addressbook, contact, nil);
      }
 }
Hossam Ghareeb
  • 7,063
  • 3
  • 53
  • 64
Inder_iOS
  • 1,636
  • 1
  • 12
  • 20
  • You want to delete the contact from your device contact ?? Or from the array ? – Janmenjaya Sep 27 '16 at 11:01
  • Delete from the device – Inder_iOS Sep 27 '16 at 11:40
  • Possible duplicate of [How can i delete contact from main AddressBook when i delete specific group in objective-c?](http://stackoverflow.com/questions/35569682/how-can-i-delete-contact-from-main-addressbook-when-i-delete-specific-group-in-o) – Ketan Parmar Sep 27 '16 at 12:12

3 Answers3

1

You can do this way

I have written the method for you, just pass the array of contacts to be deleted

My Code :

-(void)removeSlectedContactList:(CFArrayRef)arrContacts {
    ABAddressBookRef addressbook = ABAddressBookCreate();
    if (arrContacts)
    {
        int count = CFArrayGetCount(arrContacts);
        for (int i = 0; i < count; ++i)
        {
            ABRecordRef contact = CFArrayGetValueAtIndex(arrContacts, i);
            ABAddressBookRemoveRecord(addressbook, contact, nil);
        }
    }
    ABAddressBookSave(addressbook, nil);
    CFRelease(addressbook);
}

If you have name of people then loop through your array to get duplicate user lists and then delete

Here is the Sample code :

NSString *searchName = @"NameOfUser";
ABAddressBookRef addressBook = ABAddressBookCreate();
CFStringRef nameRef = (__bridge CFStringRef) searchName;
CFArrayRef  arrSearchUsers = ABAddressBookCopyPeopleWithName(addressbook, nameRef);
[self removeSlectedContactList:arrSearchUsers];
Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
  • What i pass in CFArrayRef, because i pass the NSMutable array this i got the error on ABAddressBookSave(addressbook, nil); – Inder_iOS Sep 27 '16 at 12:07
  • NO, you need to pass CFArray. I guess you have the list of people those are duplicate contact. Or else what is there in your array ?? – Janmenjaya Sep 27 '16 at 12:11
  • I have the Name of the users on array that i want to delete – Inder_iOS Sep 27 '16 at 12:17
  • Then you can get all contacts for a single name, like if "userA" is the contact name and that is saved multiple time, then you can get the array containing list of user named "userA", then call the method to delete. I have updated my question, just check and let me know – Janmenjaya Sep 27 '16 at 12:23
  • Yes, the updated code is for single name who save in multiple time, But i want multiple name to deleted – Inder_iOS Sep 27 '16 at 12:26
  • Just use for loop to get each name one by one and then use same procedure to delete. like example for(NSString *name in arrPeople){ //here the bunch of code to delete, as shown in answer} – Janmenjaya Sep 27 '16 at 12:27
  • Most Welcome, Happy to help. Up vote the answer so that other can know too. – Janmenjaya Sep 27 '16 at 12:43
  • But the code delete all those contact who have the same name, but i need delete the single contact, multiple contact but not like the all contact who have the same name – Inder_iOS Sep 27 '16 at 12:59
  • Then insode "removeSlectedContactList" method there is a for loop, put condition so as to run till the last count -1. Then it will delete all duplecate and keep only one with that name. Did you get me ? – Janmenjaya Sep 27 '16 at 13:03
  • I agree with you, but suppose i have 10 contacts of same name then i want to delete only object 5 from array then how will i deleted that object. – Inder_iOS Sep 27 '16 at 13:25
  • Then make the condition true only for 5th object, like inside the for loop of "removeSelectedContactList" – Janmenjaya Sep 27 '16 at 13:33
0

Above code remove record from Array list in memory. In order to delete from addressBook You need to call save function after this line ABAddressBookRemoveRecord(addressbook, contact, nil);

 CFErrorRef error=NULL;
 ABAddressBookSave(addressBook,&error);

Hope this helps.

Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
Ganesh Amrule
  • 407
  • 2
  • 12
0

You can try to use RHAddressBook library, I don't recommend you to use C style APIs, it is hard to work with.

Hao Xi
  • 351
  • 4
  • 12