如何使用ABAddressBook删除iPhone地址簿中的记录?

时间:2010-11-21 10:09:43

标签: iphone objective-c ios cocoa-touch abaddressbook

我正在学习地址簿框架,然后我想删除iPhone联系簿中的记录。我检查了文档并找到了一个名为ABAddressBookRemoveRecord的函数,但我找不到删除记录的方法,比如用户将选择一条记录,然后点击删除按钮,然后记录将被删除

到目前为止,我所做的一切都是对文档的抨击,而这就是全部。

您能否提供一个如何删除地址簿中记录的链接或示例?

谢谢!

3 个答案:

答案 0 :(得分:3)

ABAddressBookRemoveRecord删除记录时,应将结果保存为ABAddressBookSave。如果你想让UIInterface删除记录,我想你需要自己实现。关于apple提供的联系人的UI位于ABAddressBookUI framework

答案 1 :(得分:3)

查看ABPersonViewController + Delete类别,该类别可以在不使用任何私有方法的情况下删除联系人:

https://github.com/shrtlist/ABDelete

答案 2 :(得分:0)

目标C代码:

ABAddressBookRef addressBook; 
CFErrorRef error = NULL; 
addressBook = ABAddressBookCreate();
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook,<YOUR 'PERSON' GOES HERE>);
ABAddressBookRemoveRecord(addressBook, (ABRecordRef)person, &error );
if(error !=NULL)
{
    // Handle success

}

ABAddressBookSave(addressBook, NULL);

Swift Code:

var emptyDictionary: CFDictionaryRef?
        var addressBookRef: ABAddressBookRef?
        var err: Unmanaged<CFErrorRef>? = nil
        var userRecord: ABRecordRef?
        addressBookRef = ABAddressBookCreateWithOptions(emptyDictionary, &err)?.takeRetainedValue()
        userRecord = ABAddressBookGetPersonWithRecordID(addressBookRef, "Record ID of User").takeUnretainedValue()

        ABAddressBookRemoveRecord(addressBookRef, userRecord, &err)
        if err != nil {
            // Handle success
        }

        // Save Address Book changes
        if ABAddressBookHasUnsavedChanges(addressBookRef){
            var err: Unmanaged<CFErrorRef>? = nil
            let savedToAddressBook = ABAddressBookSave(addressBookRef, &err)
            if savedToAddressBook {
                print("Successully saved changes.")
            } else {
                print("Couldn't save changes.")
            }
        } else {
            print("No changes occurred.")
        }