. FYI , , , , , , !!
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
picker.dismiss(animated: true, completion: nil)
let identifier = contact.identifier
updateContact(contactIdentifier: identifier)
}
func updateContact(contactIdentifier: String){
let keysToFetch = [CNContactViewController.descriptorForRequiredKeys()]
let contactStore = CNContactStore()
do {
let contactToUpdate = try contactStore.unifiedContact(withIdentifier: contactIdentifier, keysToFetch: keysToFetch).mutableCopy() as! CNMutableContact
if contactToUpdate.familyName.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
contactToUpdate.familyName = "your value"
}
if contactToUpdate.givenName.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
contactToUpdate.givenName = "your value"
}
if contactToUpdate.organizationName.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
contactToUpdate.organizationName = "your value"
}
if contactToUpdate.jobTitle.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
contactToUpdate.jobTitle = "your value"
}
an existing one.
for i in contact.phoneNumbers {
contactToUpdate.phoneNumbers.append(i)
}
for i in contact.emailAddresses {
contactToUpdate.emailAddresses.append(i)
}
for i in contact.postalAddresses {
contactToUpdate.postalAddresses.append(i)
}
let contactsViewController = CNContactViewController(forNewContact: contactToUpdate)
contactsViewController.delegate = self
contactsViewController.title = "Edit contact"
contactsViewController.contactStore = contactStore
let nav = UINavigationController(rootViewController: contactsViewController)
DispatchQueue.main.async {
self.present(nav, animated: true, completion: nil)
}
}
catch {
print(error.localizedDescription)
}
}