CNContact 添加新联系人问题

CNContact add new contact issue

我在通过 Contact Framework 添加联系人时遇到问题。

我使用设备 iPhone 5s iOS 12.1.2

我添加联系人的代码如下::

let saveRequest = CNSaveRequest()
saveRequest.add(self, toContainerWithIdentifier: nil)
do {
    try contactStore.execute(saveRequest)
} catch let error {
    print("Error occurred while saving the request \(error)")
}

每次都会出现如下错误::

Error occurred while saving the request Error Domain=CNErrorDomain Code=1 "Communication Error" UserInfo={NSLocalizedDescription=Communication Error, NSLocalizedFailureReason=An error occurred while trying to communicate with the Contacts service.}

现在请教任何人有关此错误的信息吗?

我找不到任何相关信息。

要为 CNcibtactStore 添加单例,只需添加

/**
 A var to store CNContactStore
 */
let mContactStore = CNContactStore.init()

AppDelegate.swift 文件中。

要在任何地方使用它,只需使用此代码

 contactStore = appDelegate.mContactStore

这将解决我的问题,希望也能解决您的问题。