如何以编程方式删除iOS中的多个地址簿联系人?

时间:2015-02-25 05:21:00

标签: ios iphone contacts addressbook

您好我需要在tableview中显示所有地址簿联系人。如果用户想要选择多个联系人删除那些选定的联系人。为此我没有找到任何解决方案,请帮助我。

1 个答案:

答案 0 :(得分:2)

你可以试试这个:
您需要#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);
}

修改
示例:根据我的情况,我想删除所有联系人名称“Sample”

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