首页 > 解决方案 > 使用无法在 swift 3 上运行的多部手机创建新联系人

问题描述

我想用多部手机创建新联系人。我不知道为什么,但我的快速代码只创建了 1 个电话号码。与创建新联系人相关的示例很少,所以我想我的问题会对其他人有所帮助。

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)
}

谢谢

标签: swiftswift3contact

解决方案


我使用Matt Neuburg编写的示例代码Programming-iOS-Book-Examples找到了解决方案:

代替

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

解决方案是:

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

感谢 Matt Neuburg 的评论和良好的编码!


推荐阅读