Recently I have switched from old ABAddressBook framework to new CNContacts. In my project I synchronize contacts from native with my own core data contacts. For this I use contact identifier and phone identifiers to synchronize phone numbers.
But I have observed interesting thing, when i try to edit the contact, I call this line of code
func getContact() -> CNContact? {
let contactStore = CNContactStore()
guard let contactRecord = try? contactStore.unifiedContact(withIdentifier: "8222B6F1-DE99-4099-82A4-47EAB9206A94:ABPerson", keysToFetch: [CNContactViewController.descriptorForRequiredKeys()]) else {
return nil
}
return contactRecord
}
@IBAction func showContact() {
let contactViewController = CNContactViewController(forNewContact: self.getContact())
contactViewController.delegate = self
contactViewController.title = "New Contact"
let navigationController = UINavigationController(rootViewController: contactViewController)
navigationController.navigationBar.isTranslucent = false
self.present(navigationController, animated: true, completion: nil)
}
func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
let newContact = self.getContact()
viewController.dismiss(animated: false, completion: nil)
}
It is really simple. But if in CNContactViewController I edit user photo, phone identifiers will change, even though I did no editing to phone numbers in this controller. The phone identifier can easily be printed like this:
po newContact?.phoneNumbers.first?.identifier
This really messes up my sync, since user will maybe just change photo, but identifiers for phone numbers will change, and I will have no way to know what happened. This identifier will not change if I edit some other data, like persons name, company etc... It will remain the same even if I edit the phone to some other value. But for some reason changing the photo messes it completely up.
Has anyone else observed this?
Here is link to sample project to test this https://drive.google.com/file/d/0B9ngBRq15jSuZTBYNVJCaVJ5WGc/view?usp=sharing
EDIT: I tested this on real phone