0

我现在搜索了至少 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)
}

谢谢你的帮助!

4

2 回答 2

1

但我不想要这个。我可以抑制这种行为吗?我很确定必须有一个解决方案。

没有。如果用户保存编辑的联系人(点击完成),那么当您听到它时,它已经保存到联系人数据库中,您无能为力。

当然你也可以右转删除已保存的联系人。但是你不能一开始就阻止储蓄的发生。

于 2019-02-27T04:03:37.623 回答
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
    }
}
于 2019-08-14T09:42:58.360 回答