Is it possible to detect deleted Address Book entries (for example from the iOS phonebook itself) from within an application?
I have an application which needs to synchronize a local copy of the iOS Address Book database with the Address book from the phone. The application supports background mode on iOSes which support it.
Testing the application in a background capable iOS environment my problem is the following:
The application does not detect an entry which has been deleted in the system address book until the application is closed completely (iOS 4 killing it with the red X in springboard). If the application is simply sent to the background, a contact is deleted from the system address book, and then the app brought to the foreground again the function ABRecordRef() for开发者_JS百科 the deleted entry id does not return NULL as would be expected for an entry which no longer exists, it returns the entry as if it had never been removed from the system address book database.As above - if the application is completely terminated and restarted then ABRecordRef() for the deleted id works as expected - it returns NULL. Is there any way to detect this deleted entry without waiting for the app to be killed and restarted?
Yes. Register for changes to the address book with ABAddressBookRegisterExternalChangeCallback
, e.g.
// Your method called when the user has granted access to their address book data.
- (void)accessGrantedForAddressBook
{
ABAddressBookRegisterExternalChangeCallback(sharedAddressBook, MyAddressBookExternalChangeCallback, (__bridge void *)(self));
}
Where MyAddressBookExternalChangeCallback
is your callback function.
I belive that you need to create a new ABAddressBook every time you refresh data from the address book. The reason is this:
You might have added, updated and removed records in your copy of the address book but not yet called Save or Revert. Hence, if the system (e.g. iOS build in contact book app) changes things in the address book, you cannot get them automatically.
Try either running Revert or to recreate the addressbook and it should work.
精彩评论