Skip to content

Instantly share code, notes, and snippets.

@d0z0
Created June 22, 2017 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d0z0/bd46e5ec6208c49949ed79b8a66f0543 to your computer and use it in GitHub Desktop.
Save d0z0/bd46e5ec6208c49949ed79b8a66f0543 to your computer and use it in GitHub Desktop.
Twilio Connection
//
// TwilioChatManager.m
// Floh
//
// Created by Schubert on 01/02/17.
// Copyright © 2017 Floh Network Private Limited. All rights reserved.
//
#import "TwilioChatManager.h"
#import "API.h"
#import "HeaderFile.h"
#import "Utilities.h"
@interface TwilioChatManager() <TwilioChatClientDelegate, TwilioAccessManagerDelegate>
@property (nonatomic, strong) TwilioAccessManager *accessManager;
@property (nonatomic, strong) TwilioChatClient *client;
@end
@implementation TwilioChatManager
+ (instancetype)sharedInstance {
static TwilioChatManager *sharedMyInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyInstance = [[self alloc] init];
});
return sharedMyInstance;
}
- (void)logout {
[self.client shutdown];
self.client = nil;
}
- (void)establishConnection {
if(![Utilities twilioChatEnabled]) {
return;
}
if(self.client) {
[self logout];
}
[TwilioChatClient setLogLevel:TCHLogLevelDebug];
__weak typeof(self) weakSelf = self;
[[API sharedInstance] createTwilioAuthTokenForDeviceId:[Utilities getValueForKeyChainStorageVarKey:DEVICE_ID] onSuccess:^(TwilioAuthResponse *response) {
TwilioChatClientProperties *properties = [[TwilioChatClientProperties alloc] init];
[TwilioChatClient chatClientWithToken:response.token properties:properties delegate:self completion:^(TCHResult *result, TwilioChatClient *chatClient) {
if([result isSuccessful]) {
self.client = chatClient;
self.accessManager = [TwilioAccessManager accessManagerWithToken:response.token delegate:weakSelf];
__weak typeof(weakSelf.client) weakClient = weakSelf.client;
[self.accessManager registerClient:self.client forUpdates:^(NSString * _Nonnull updatedToken) {
[weakClient updateToken:updatedToken completion:^(TCHResult *result) {
if (![result isSuccessful]) {
NSLog(@"Error in updating token auth token");
}
}];
}];
}
else {
NSLog(@"Error in initializing chat client");
}
}];
} onError:^(NSError *error) {
NSLog(@"Error in creating twilio auth token");
}];
}
- (void)chatClient:(TwilioChatClient *)client errorReceived:(TCHError *)error {
NSLog(@"Twilio Client Error Code=%ld, Description=%@", (long)error.code, error.localizedDescription);
}
# pragma mark TwilioAccessManagerDelegate
- (void)accessManagerTokenExpired:(TwilioAccessManager *)accessManager {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[self updateTokenForAccessManager:accessManager];
});
}
- (void)updateTokenForAccessManager:(TwilioAccessManager *)accessManager {
[[API sharedInstance] createTwilioAuthTokenForDeviceId:[Utilities getValueForKeyChainStorageVarKey:DEVICE_ID] onSuccess:^(TwilioAuthResponse *response) {
[accessManager updateToken:response.token];
} onError:^(NSError *error) {
NSLog(@"Error while trying to get new access token");
}];
}
- (void)accessManagerTokenWillExpire:(TwilioAccessManager *)accessManager {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[self updateTokenForAccessManager:accessManager];
});
}
- (void)accessManagerTokenInvalid:(TwilioAccessManager *)accessManager {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[self updateTokenForAccessManager:accessManager];
});
}
- (void)accessManager:(TwilioAccessManager *)accessManager error:(NSError *)error {
NSLog(@"Access manager error: %@", [error localizedDescription]);
}
- (void)chatClient:(TwilioChatClient *)client synchronizationStatusUpdated:(TCHClientSynchronizationStatus)status {
// an observer to above Notification on a different controller will start querying a set of
// cached channel sids using [client.channelsList channelWithSidOrUniqueName:]
// if the status reaches TCHClientSynchronizationStatusChannelsListCompleted
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:TwilioSynchronizationStatusUpdated object:nil userInfo:@{@"syncStatus": @(status)}];
});
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment