Question

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!

Was it helpful?

Solution

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

https://github.com/shrtlist/ABDelete

OTHER TIPS

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.

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.")
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top