How to delete record in iPhone address book using ABAddressBook?

℡╲_俬逩灬. 提交于 2019-12-22 09:46:07

问题


I was learning the addressbook framework and then I wanted to delete the record from the the iPhone contact book. I checked up the documentation and found a function called ABAddressBookRemoveRecord, but I can't find out a way to delete the records, like the user will select a record and then hit the delete button and then the record will get deleted.

All I did till now is banged my head over the documentation and that's all.

Can you please provide me a link or an example how to delete a record in the address book?

Thank You!


回答1:


Check out the ABPersonViewController+Delete category which enables contact deletion without using any private methods:

https://github.com/shrtlist/ABDelete




回答2:


When you delete a record by ABAddressBookRemoveRecord, you should save the final result by ABAddressBookSave. If you want a UIInterface to delete the record, I think you need to implement by yourself. The UIs about contacts provided by apple are inside the ABAddressBookUI framework.




回答3:


Objective C code:

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.")
        }


来源:https://stackoverflow.com/questions/4237332/how-to-delete-record-in-iphone-address-book-using-abaddressbook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!