Skip to main content
Version: 4.38.0

Configuration

Initialization

In the application:didFinishLaunchingWithOptions: method, configure an SDK Threads instance:

  • configureTransportProtocol - Message delivery protocol:
info

You need to get the following configuration parameters from your implementation manager:

  • providerUid - App ID
  • webSocketURL - URL to connect to the chat through WebSocket API
  • restURL - URL for calls to REST API
  • dataStoreURL - URL to upload files via DataStore API
danger

When configuring SDK via THRAttributes, all NON-UI settings must be set before calling the configureTransportProtocol method!

For example:

threads.attributes.logLevels = .all
threads.attributes.allowUntrustedSSLCertificate = true
import Threads

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

// Step 1: Configure Threads Framework
let threads = Threads.threads()
threads.attributes.logLevels = .all

threads.configureThreadsGateTransportProtocol(
with: self,
providerUid: "PROVIDER_UID",
webSocketURL: URL(string: 'URL to WebSocket API')!,
restURL: URL(string: 'URL to REST API')!,
dataStoreURL: URL(string: 'URL to DataStore API'))!

// Step 2: Register device for remote notifications
UNUserNotificationCenter.current().delegate = self

Threads.threads().registerApplicationForRemoteNotificationsStandartOptions(authorizationStatusDenied: {
// Handle deny notifications
}) { deviceToken in
// Handle received device token if needed
}

return true
}

User Installation

Use the following method: setClientWithId:name:data:appMarker:signature:

let clientInfo = THRClientInfo(clientId: CLIENT_ID)
clientInfo.name = CLIENT_NAME
clientInfo.data = CLIENT_DATA
clientInfo.appMarker = APP_MARKER
clientInfo.signature = SIGNATURE
Threads.threads().setClientInfo(clientInfo)

where:

  • CLIENT_ID - Unique client's identifier. This parameter is required.
  • CLIENT_NAME - Client's name. This parameter is optional.
  • CLIENT_DATA - JSON string with client data. This parameter is optional.
  • APP_MARKER - Identifier for multichat; pass null only, if not used.
  • SIGNATURE - Authorization signature clientId. At the moment, this parameter is optional, verification is enabled on the Threads server. The signature must be generated on your authorization server based on clientId using the RSA private key, then encrypted in Base64. For the general scheme of working with the signature, refer to the backend documentation.
danger

CLIENT_ID must be unique and always refer to the same user. Avoid using phone numbers, email addresses, and other identifiers linked to user data as an identifier.

User's Logout

Logout must be executed when the app is supposed to stop receiving messages for users. For example, when users log out of the app.

caution

Upon clientId change, automatic logout is not executed.

Threads.threads().logout()
или
Threads.threads().logout(withClientId: clientId)

Show Chat Screen

To show the chat screen, you need to receive the chatViewController parameter  with indication of appearance parameters THRAttrubutes. The received controller can be displayed in any native way:

let attributes = THRAttributes()
let vc = Threads.threads().chatViewController(with: attributes)
navigationController?.pushViewController(vc, animated: true)

How to Open Chat Screen

Examples in the demo app

All chat opening options are implemented on Objective-C and Swift in the corresponding examples in the  IntegrationsViewController class.

1. Via Push Method in Code

How to open the chat screen using the Push method in current UINavigationController.

let vc = Threads.threads().chatViewController(with: attributes)
navigationController?.pushViewController(vc, animated: true)

2. Via Present Method in Code

In this case, you need to place the chat in UINavigationController.

let vc = Threads.threads().chatViewController(with: attributes)
let nc = UINavigationController(rootViewController: vc)
present(nc, animated: true, completion: nil)

3. Via Show Method in Storyboard

To open the chat screen using the Show method (for example, Push), you need to:

  1. Create a child of ChatNavigationController from UINavigationController.
  2. Set the created class as Custom Class for the required scene of UINavigationController in Storyboard.
  3. Create a Segue connection to the created scene of ChatNavigationController. Example of ChatNavigationController implementation:
class ChatNavigationController.m: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()

self.viewControllers = [self.getChatViewController()]
}

func getAttributes() -> THRAttributes {
let attributes = THRAttributes()
attributes.showWaitingForSpecialistProgress = false
return attributes
}

func getChatViewController() -> UIViewController {
let attributes = getAttributes()
let chatViewController = Threads.threads().chatViewController(with: attributes)
return chatViewController
}
}

4. Integration on UITabBarController Tab in Code

Integration method is similar to chat opening using the Present method, since you need to place the chat into UINavigationController.

let vc = Threads.threads().chatViewController(with: attributes)

let nc = UINavigationController(rootViewController: chatViewController)
nc.tabBarItem = UITabBarItem(title: NSLocalizedString("Chat", comment: ""), image: UIImage(named: "tabBarItemChat"), tag: 0)

let tabBarController = UITabBarController()
tabBarController.viewControllers = [nc]

5. Integration on UITabBarController Tab in Storyboard

To integrate the chat as a tab via Storyboard, you need ChatNavigationController from How to Open Chat Screen via Show Method in Storyboard.

  1. Set the scene of UINavigationController
  2. Set Custom Class - ChatNavigationController,
  3. Create a connection of viewControllers from UITabBarController to ChatNavigationController.