Sync Phonebook Contact

Sync Contact#


It will be only applicable for mobile number registration and contact book synchronization.

If mobile number is used as a primary id for communication in chat sdk then there has to be sync between user and server. The following code block initiate the contact sync and let the user communicate with their contacts who uses the client app too.

Note : In order to sync mobile contacts need to enable Contact syncing using ChatManager.enableContactSync() method

    ContactSyncManager.shared.syncContacts(){ isSuccess, flyError, flyData in
    var data = flyData
    if isSuccess {
    // Contact synced successfully update the UI
    } else{
    print(data.getMessage() as! String)
    CALLBACKFlyCompletionHandlerFlyCompletionHandler used as completion Handler

    Note : To access phone contacts add the Privacy - Contacts Usage Description key and proper description for contact access request value, so the ContactSyncManager class can read phone's contacts for syncing.

    Observe Contact Sync#

    The progress of contact syncing can be observed using the NotificationCenter using the name of FlyConstants.contactSyncState. From the notification's userInfo get the status of the progress using the key FlyConstants.contactSyncState, which gives a string value which can be used as a raw value for the enum type ContactSyncState. The code snippet below shows the show the observer and handling of contact sync progress.

      NotificationCenter.default.addObserver(self, selector: #selector(self.contactSyncCompleted(notification:)), name: NSNotification.Name(FlyConstants.contactSyncState), object: nil)
      @objc func contactSyncCompleted(notification: Notification){
      if let contactSyncState = notification.userInfo?[FlyConstants.contactSyncState] as? String {
      switch ContactSyncState(rawValue: contactSyncState) {
      case .inprogress:
      //Update the UI
      case .success:
      //Update the UI
      case .failed:
      //Update the UI

      Once contact sync is completed successfully, ContactManager.shared.getRegisteredUsers() method will be called internally to fetch the profile data of the phone contacts.

      Note : Remove the Observer for Contact Sync properly to avoid memory leaks.