How to delete an entry in the iPhone address book using ABAddressBook?

I studied the structure of the address book, and then I wanted to delete the entry from the iPhone contact book. I checked the documentation and found a function called ABAddressBookRemoveRecord, but I can’t find a way to delete the entries, for example, the user will select the entry and then click the delete button, and then the entry will be deleted.

Everything I have done so far has hit my head on the documentation and that’s it.

Can you provide me a link or an example of how to delete an address book entry?

Thank!

+3
source share
3 answers

ABPersonViewController + Delete, , :

https://github.com/shrtlist/ABDelete

+3

ABAddressBookRemoveRecord, ABAddressBookSave. , UIInterface , , . , , 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.")
        }
0
source

Source: https://habr.com/ru/post/1776063/


All Articles