Search code examples
iosiphonecontactsaddressbook

How can i delete multiple address book contacts in iOS programmatically?


Hi i have a requirement to show all address book contacts in tableview. if the user wants to select the multiple contacts delete those selected contacts.for this i didn't found any solution for that please help me.


Solution

  • You could try this:
    You need #import <AddressBookUI/AddressBookUI.h>:

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

    EDIT:
    Example: As my case I want to remove all contacts have name "Sample"

    NSString *searchName = @"Sample";
    ABAddressBookRef addressbook = ABAddressBookCreate();
    CFStringRef nameRef = (__bridge CFStringRef) searchName;
    CFArrayRef  allSearchRecords = ABAddressBookCopyPeopleWithName(addressbook, nameRef);
    [self removeContactWithRecordsList:allSearchRecords];