Configuration
Initialization
In the application:didFinishLaunchingWithOptions:
method, configure an SDK Threads instance:
configureTransportProtocol
- Message delivery protocol:
You need to get the following configuration parameters from your implementation manager:
providerUid
- App IDwebSocketURL
- URL to connect to the chat through WebSocket APIrestURL
- URL for calls to REST APIdataStoreURL
- URL to upload files via DataStore API
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
- Swift
- Objective-C
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
}
#import <Threads/Threads.h>
@interface AppDelegate () <UNUserNotificationCenterDelegate, ThreadsDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Step 1: Configure Threads Framework
Threads *threads = [Threads threads];
threads.isDebugLoggingEnabled = YES;
[threads configureThreadsGateTransportProtocolWithDelegate:self
providerUid:@"PROVIDER_UID"
webSocketURL: [NSURL URLWithString:@"URL to WebSocket API"]
restURL: [NSURL URLWithString:@"URL to REST API"]
dataStoreURL: [NSURL URLWithString:@"URL to DataStore API"]];
// Step 2: Register device for remote notifications
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
[[Threads threads] registerApplicationForRemoteNotificationsStandartOptionsWithAuthorizationStatusDenied:^{
// Handle deny notifications
} completionHandler:^(NSData * _Nullable deviceToken) {
// Handle received device token if needed
}];
return YES;
}
User Installation
Use the following method: setClientWithId:name:data:appMarker:signature:
- Swift
- Objective-C
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)
THRClientInfo *clientInfo = [[THRClientInfo alloc] initWithClientId: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; passnull
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.
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.
Upon clientId
change, automatic logout is not executed.
- Swift
- Objective-C
Threads.threads().logout()
или
Threads.threads().logout(withClientId: clientId)
[[Threads threads] logout];
или
[[Threads threads] logoutWithClientId:client.id];
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:
- Swift
- Objective-C
let attributes = THRAttributes()
let vc = Threads.threads().chatViewController(with: attributes)
navigationController?.pushViewController(vc, animated: true)
THRAttributes *attributes = [[THRAttributes alloc] init];
UIViewController *vc = [[Threads threads] chatViewControllerWithAttributes:attributes];
[self.navigationController pushViewController:vc animated:YES];
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
.
- Swift
- Objective-C
let vc = Threads.threads().chatViewController(with: attributes)
navigationController?.pushViewController(vc, animated: true)
UIViewController *vc = [[Threads threads] chatViewControllerWithAttributes:attributes];
[self.navigationController pushViewController:vc animated:YES];
2. Via Present Method in Code
In this case, you need to place the chat in UINavigationController
.
- Swift
- Objective-C
let vc = Threads.threads().chatViewController(with: attributes)
let nc = UINavigationController(rootViewController: vc)
present(nc, animated: true, completion: nil)
UIViewController *vc = [[Threads threads] chatViewControllerWithAttributes:attributes];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:chatViewController];
[self presentViewController:nc animated:YES completion:nil];
3. Via Show Method in Storyboard
To open the chat screen using the Show method (for example, Push), you need to:
- Create a child of
ChatNavigationController
fromUINavigationController
. - Set the created class as
Custom Class
for the required scene ofUINavigationController
in Storyboard. - Create a
Segue
connection to the created scene ofChatNavigationController
. Example ofChatNavigationController
implementation:
- Swift
- Objective-C
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
}
}
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
.
- Swift
- Objective-C
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]
UIViewController *vc = [[Threads threads] chatViewControllerWithAttributes:attributes];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
nc.tabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"Chat", "") image:[UIImage imageNamed:@"tabBarItemChat"] tag:0];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
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.
- Set the scene of
UINavigationController
- Set
Custom Class
-ChatNavigationController
, - Create a connection of
viewControllers
fromUITabBarController
toChatNavigationController
.