Inderpal Singh Inderpal Singh - 2 months ago 10
iOS Question

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

Answer

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