I have written an app that gets access to addressbook
, the user is supposed to be able to delete a contact from the app and it will delete that contact from the addressbook
Sometimes this just doesn't happen, although when tracing, everything works as expect and I get notified that ABAddressBookRegisterExternalChangeCallback();
right after the delete, but when I go to contact app on the phone, the total number of contacts remain the same and that contact is still there, not deleted.
Here is the code for deletion
// this method takes and NSManagedObject which holds the id of the person that should be deleted
- (void)deleteContactFromAddressBook:(NSManagedObject *)object
{
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
NSNumber *contactID = [object valueForKey:@"contactID"];
ABRecordRef contactRef = ABAddressBookGetPersonWithRecordID(addressBook, [contactID intValue]);
NSString *firstName = (__bridge NSString *)ABRecordCopyValue(contactRef,
kABPersonFirstNameProperty);
NSString *lastName = (__bridge NSString *)ABRecordCopyValue(contactRef,
kABPersonLastNameProperty);
if (!lastName) {
lastName = @"";
}
NSLog(@"contact to be deleted - Name: %@" ,[firstName stringByAppendingString:lastName]);
BOOL deleteCheck = ABAddressBookRemoveRecord(addressBook, contactRef, NULL);
BOOL saveCheck = ABAddressBookSave(addressBook, NULL);
NSLog(@"delete check = %hhd ---------- save check = %hhd", deleteCheck, saveCheck);
NSLog(@"Contact Deleted with id: %@", contactID);
}
If you need further explanation don't hesitate to ask. I am really frustrated by this unexpected behavior and I have no clue where to look.
Thank you.