我现在搜索了至少2个小时,却找不到答案。
我需要具有CNContact
类型的对象。我找到了CNContactViewController
,此视图完全可以满足我的要求。有了它,我可以使用预定义的视图创建新的联系人,这给了我一个CNContact
对象。
这是我的问题:
CNContactViewController
将创建的联系人保存到contactStore
。但我不要这个。我可以抑制这种行为吗?我很确定一定有解决方案。
这是我创建ViewController
的代码:
let contactController = CNContactViewController(forNewContact: nil)
contactController.allowsEditing = true
contactController.allowsActions = false
contactController.displayedPropertyKeys = [CNContactPostalAddressesKey, CNContactPhoneNumbersKey, CNContactGivenNameKey]
contactController.delegate = self
contactController.view.layoutIfNeeded()
let navigationController = UINavigationController(rootViewController: contactController)
self.present(navigationController, animated: true)
在这里,我想不使用通讯录中的联系人:
func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
viewController.navigationController?.dismiss(animated: true)
}
感谢您的帮助!
答案 0 :(得分:1)
但是我不想要这个。我可以抑制这种行为吗?我很确定一定有解决方案。
没有。如果用户保存了已编辑的联系人(完成的轻击),那么到您听到它时,该联系人已保存到联系人数据库中,您将无能为力。
当然,您可以右转并删除保存的联系人。但是您不能阻止保存从一开始就进行。
答案 1 :(得分:0)
我正在做这样的事情:
let contact = CNMutableContact()
contact.contactType = .person
contact.givenName = "giveName"
let cvc = CNContactViewController(forUnknownContact: contact)
cvc.delegate = self
cvc.contactStore = CNContactStore()
cvc.allowsEditing = false
self.navigationController?.pushViewController(cvc, animated: true)
您可以允许将联系人保存到内部联系人存储中-但不会自动存储。
记住要实现委托功能(fx作为扩展)
extension MyContactsViewController: CNContactViewControllerDelegate {
func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
viewController.dismiss(animated: true, completion: nil)
}
func contactViewController(_ viewController: CNContactViewController, shouldPerformDefaultActionFor property: CNContactProperty) -> Bool {
print(property.key)
return true
}
}