Inder_iOS
Inder_iOS

Reputation: 1656

How to delete multiple contacts from AddressBook

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

Upvotes: 2

Views: 169

Answers (3)

Janmenjaya
Janmenjaya

Reputation: 4163

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];

Upvotes: 1

Hao Xi
Hao Xi

Reputation: 381

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

Upvotes: 0

Ganesh Amrule
Ganesh Amrule

Reputation: 427

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.

Upvotes: 0

Related Questions