iosswiftcncontactcncontactstore

CNContact add new contact issue


I'm faccing an issue at time of adding contacts throught Contact Framework.

I used device iPhone 5s with iOS 12.1.2

My Code for adding contact is as below ::

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

This everytime thows error as below ::

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.}

Dose any one now anything about this error?

I'm not able to found any thing about this.


Solution

  • To add the singleton for the CNcibtactStore just add

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

    Inside AppDelegate.swift file.

    And to use it anywhere just use this code

     contactStore = appDelegate.mContactStore
    

    This will solve the issue for me and hopefully will solve the issue for you too.