ios - CNContactViewController() 的 "Create New Contact"和 "Add to Existing Contact"

标签 ios swift contacts cncontact

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

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

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

}

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

最佳答案

您一直不显示您的 真实代码,因此无法为您提供帮助。所以我失去了兴趣。我将只向您展示的真实代码,让您去研究它并思考我正在做的事情和您正在做的事情之间的区别。这是实际的工作代码;去吧,做同样的事情:

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)

enter image description here

关于ios - CNContactViewController() 的 "Create New Contact"和 "Add to Existing Contact",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39192198/

相关文章:

ios - 在代码片段示例中使用__autoreleasing

ios - iOS 使用什么算法进行增量更新

ios - 具有透明背景图像的 UIButton 无法正常工作

ios - iPhone 的 UIButton 的 UIPopOver

iphone - 我可以构建一个在 iPhone 上编辑联系人列表的应用程序吗?

android - 获取电话簿到应用程序

ios - 变换旋转后如何找到UIView的框架

ios - 从 UITableViewCell 更新 UIViewController 中数组的对象

ios - Xcode8 : Why Xcode8 is not building/compiling after converting to Swift3 syntax's

android - Android上联系人的上次修改时间