Faustino Gagneten
Faustino Gagneten

Reputation: 2774

create new contact with multiple phones not working on swift 3

I want to create new contact with multiple phones. I don't know why but my swift code creates only 1 phone number. There are few examples related to create a new contact, so I guess my question will help others.

private func createNewContact(myContact : AgregarContactoViewModel) {
    let store = CNContactStore()
    let contact = CNMutableContact()

    // Name
    contact.familyName = myContact.getName()

    // Phones
    for i in (0..<myContact.getPhones().count) {
       let phone = CNLabeledValue(label: CNLabelOther, value: CNPhoneNumber(stringValue: myContact.getPhones()[i]))
       contact.phoneNumbers.append(phone)
    }

    // Call the controller and create new contact
    let controller = CNContactViewController(forNewContact : contact)
    controller.contactStore = store
    controller.delegate = self
    self.navigationController?.setNavigationBarHidden(false, animated: true)
    self.navigationController?.pushViewController(controller, animated: true)
}

Thanks

Upvotes: 0

Views: 56

Answers (1)

Faustino Gagneten
Faustino Gagneten

Reputation: 2774

I found the solution using the example code Programming-iOS-Book-Examples written by Matt Neuburg:

Instead of

let phone = CNLabeledValue(label: CNLabelOther, value: CNPhoneNumber(stringValue: myContact.getPhones()[i]))
   contact.phoneNumbers.append(phone)

the solution was:

   contact.phoneNumbers.append(CNLabeledValue(label: "phone", value: CNPhoneNumber(stringValue: myContact.getPhones()[i])))

Thanks Matt Neuburg for your comments and good coding!

Upvotes: 1

Related Questions