iOS联系人框架-如何保存/创建新的联系人存储

时间:2018-10-16 10:15:34

标签: ios swift contacts

我希望以编程方式创建一个新的联系人存储并将联系人保存到Swift中。

尝试保存联系人并使用toContainerWithIdentifier:时,我已将字符串值传递给容器标识符,但是我收到以下错误:

  

线程1:致命错误:“尝试!”表达式意外引发错误:   错误域= CNErrorDomain代码= 200“不存在更新的记录”   UserInfo = {CNInvalidRecordIdentifiers =(       Local),NSLocalizedDescription =更新记录不存在,NSLocalizedFailureReason =保存请求失败,因为它更新了   记录不存在或已被删除。}

如果我使用此代码toContainerWithIdentifier:nil,则联系人可以正确地保存在默认存储中,因此我知道我的保存代码没有问题。

这是链接到按钮的相当基本的测试。如果我在toContainerWithIdentifier中使用了nil以外的其他任何内容,则会收到错误消息。

    @IBAction func testAdd(_ sender: UIButton) {
    contact.givenName = "John"
    contact.familyName = "Appleseed"
    let store = CNContactStore()
    let saveRequest = CNSaveRequest()
    saveRequest.add(contact, toContainerWithIdentifier:nil)
    try! store.execute(saveRequest)
}

1 个答案:

答案 0 :(得分:1)

  

创建新的联系人存储

您不能。整个概念没有任何意义。这就是为什么它失败了。您可以保存到的任何商店必须已经存在。除了默认存储nil之外,您没有其他理由使用任何标识符。

相关问题