问题
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