Create New Contact and Add to Existing Contact for CNContactViewController ()

With ABAddressBook, when I wanted the user to be able to "Create a new contact" and "Add to an existing contact" for a contact that they had not seen before, I would create and introduce ABUnknownPersonViewController .

I cannot find a way to replicate this functionality within CNContacts. It seemed to me that CNContactViewController(forUnknownContact: contact) could work, but, unfortunately, this allows the user to "Send a message" or "Share a contact."

How can I allow a user to save a contact in my address book, either as a new contact, or as part of an existing one, in CNContacts?

 func presentContact() { let status = CNContactStore.authorizationStatusForEntityType(.Contacts) switch status { case .Authorized: () case .NotDetermined: requestAccess() case .Denied, .Restricted: accessDenied() } print("authorized? \(status == .Authorized)") //prints "authorized? true" let unknown = CNContactViewController(forUnknownContact: contact!) unknown.delegate = self self.navigationController?.pushViewController(unknown, animated: false) } 

Even when I try to request access, the user still cannot save the contact.

+6
source share
2 answers

You do not show your real code, so it won’t help you. Therefore, I have lost interest. I will just show you my real code and leave you to study it and think about the difference between what I do and what you do. Here is the real working code; go and do the same:

 let con = CNMutableContact() con.givenName = "Johnny" con.familyName = "Appleseed" con.phoneNumbers.append(CNLabeledValue( label: "woods", value: CNPhoneNumber(stringValue: "555-123-4567"))) let unkvc = CNContactViewController(forUnknownContact: con) unkvc.message = "He knows his trees" unkvc.contactStore = CNContactStore() unkvc.delegate = self unkvc.allowsActions = false self.navigationController?.pushViewController(unkvc, animated: true) 

enter image description here

+11
source

What you are missing in the code is the contactStore property of the contactStore variable for the CNContactStore descriptor.

 [...] unknown.contactStore = CNContactStore() [...] 
0
source

All Articles