CNContactViewController()的“创建新联系人”和“添加到现有联系人”

时间:2021-03-08 15:42:56

With ABAddressBook, when I wanted the user to be able to have the options of "Create New Contact" and "Add to Existing Contact" for a contact they hadn't seen before, I would create and present an ABUnknownPersonViewController.

使用ABAddressBook,当我希望用户能够为他们之前从未见过的联系人选择“创建新联系人”和“添加到现有联系人”时,我将创建并呈现一个ABUnknownPersonViewController。

I can find no way to replicate this functionality in the CNContacts framework. It seemed to me that CNContactViewController(forUnknownContact: contact) could work, but unfortunately this only lets the user "Send Message" or "Share Contact."

我无法在CNContacts框架中找到复制此功能的方法。在我看来,CNContactViewController(forUnknownContact:contact)可以工作,但不幸的是,这只允许用户“发送消息”或“共享联系人”。

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

如何允许用户将联系人保存到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 can't save the contact.

即使我尝试请求访问,用户仍然无法保存联系人。

2 个解决方案

#1


12  

You keep not showing your real code, so it's impossible to help you. So I've lost interest. I'll just show you my real code instead and leave you to study it and think about the difference between what I'm doing and what you're doing. Here's actual working code; go ye and do likewise:

你一直没有显示你的真实代码,所以无法帮助你。所以我失去了兴趣。我只会告诉你我的真实代码然后让你去研究它并思考我正在做什么和你在做什么之间的区别。这是实际的工作代码;你们也这样做:

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)

CNContactViewController()的“创建新联系人”和“添加到现有联系人”

#2


0  

What you're missing in your code is setting the contactStore property of your unknown variable to a handle of a CNContactStore.

您在代码中缺少的是将未知变量的contactStore属性设置为CNContactStore的句柄。

[...]

unknown.contactStore = CNContactStore()

[...]

#1


12  

You keep not showing your real code, so it's impossible to help you. So I've lost interest. I'll just show you my real code instead and leave you to study it and think about the difference between what I'm doing and what you're doing. Here's actual working code; go ye and do likewise:

你一直没有显示你的真实代码,所以无法帮助你。所以我失去了兴趣。我只会告诉你我的真实代码然后让你去研究它并思考我正在做什么和你在做什么之间的区别。这是实际的工作代码;你们也这样做:

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)

CNContactViewController()的“创建新联系人”和“添加到现有联系人”

#2


0  

What you're missing in your code is setting the contactStore property of your unknown variable to a handle of a CNContactStore.

您在代码中缺少的是将未知变量的contactStore属性设置为CNContactStore的句柄。

[...]

unknown.contactStore = CNContactStore()

[...]