Skip to content

Chat SDK iOS: Adding Chat SDK to your existing project

simonsmiley64 edited this page May 9, 2017 · 14 revisions

When using the Chat SDK you can either add it to a current project or build your project using the Chat SDK as a foundation. In this tutorial we will be looking at adding the ChatSDK to your custom project.

Before you start we recommend watching the following video which runs through the entire process.

Add Chat SDK to custom project

Adding the Chat SDK files to your project

First we need to add the Chat SDK files. Copy the development pods and ChatSDK.podspec file into your project. At the time of this being written the development pods are as below (These may change depending on the features you are using):

  • ChatSDK
  • ChatSDKFirebase
  • ChatSDKModules
  • Docs

Note: You can easily work out which files need to be added to your project. The ChatSDK Demo project is essentially a blank project with the Chat SDK added to it. This means you need to add all the folders outside of the example project.

Note: We recommend adding a copy of the rules.json, README.md and the LICENSE.txt so you have a copy contained in your project

Now load up your Podfile and make sure the target is pointing to the correct project. This is the example from our test project:

source 'https://github.com/CocoaPods/Specs.git'

target 'TestProject' do

  pod "ChatSDKCore”, :path => “../ChatSDK/ChatSDKCore”
  pod "ChatSDKUI”, :path => "../ChatSDK/ChatSDKUI”
  pod "ChatSDKCoreData”, :path => "../ChatSDK/ChatSDKCoreData”
  pod "ChatSDKFirebaseAdapter”, :path => "../ChatSDK/ChatSDKFirebaseAdapter”

  #pod "ChatSDKModules/Backendless", :path => "ChatSDKModules"

end

Note: It is important to understand what the code here actually means as some users have had difficulty in linking in their podfile. Think of this file being a link between the podfile to the Chat SDK podspecs. You need to create a link between the two, you can use ../ to go out of a folder and FolderName to go into a folder.

Note: If you want to use Backendless for push notifications you can use the following tutorial to set up it.

Load up the terminal and change the folder to the one containing your project. Call the below code to update the pods:

pod install

Note: After calling pod install you might get a lot of yellow writing appearing in your console. This is normally and is an issue with Cocoapods - it can be ignored

Navigate to the Xcode -> ChatSDK Demo folder and copy the BTwitterHelper files and the GoogleService-Info.plist into your main project target folder.

Now load up both your project and the ChatSDK (remember to load the xcworkspace files for both). We need to first drag the BTwitterHelper and GoogleServices.Info.plist into the main project target.

Finally we need to make some additions to the project info.plist.

  • Open the Info.plist in the ChatSDK
  • Copy the chat_sdk row.
  • Copy the URL types row
  • Copy the App Transport Security Settings row
  • Copy the all the Privacy rows
  • Copy the FacebookAppID row

Note: You can click a row, then CTRL + C, click the bottom of your project Info.plist and press CTRL + V to save a lot of time copying the rows over directly.

Initialising Chat SDK in your project

Now we need to add some code to the app delegate:

First, we add the required imports to the AppDelegate.m file:

#import "BTwitterHelper.h"
#import <ChatSDK/ChatCore.h>
#import <ChatSDK/ChatUI.h>
#import <ChatSDK/ChatFirebaseAdapter.h>
#import <ChatSDK/ChatCoreData.h>

Then add the following code to the start of your didFinishLaunchingWithOptions function:

// Configure app for Facebook login
    [FIRApp configure];
    [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
    
    // Start the twitter helper to handle login
    [BTwitterHelper sharedHelper];
    
    // Set the default interface manager
    [BInterfaceManager sharedManager].a = [[BDefaultInterfaceAdapter alloc] init];
    
    // Create a network adapter to communicate with Firebase
    // The network adapter handles network traffic
    BFirebaseNetworkAdapter * adapter = [[BFirebaseNetworkAdapter alloc] init];
        
    // Set the login screen
    // This screen is customizable - for example if you are using the
    // Two factor authentication module
    adapter.auth.challengeViewController = [[BLoginViewController alloc] initWithNibName:Nil bundle:Nil];
    
    // Set the adapter
    [BNetworkManager sharedManager].a = adapter;
    
    // Set the data handler
    // The data handler is responsible for persisting data on the device
    [BStorageManager sharedManager].a = [[BCoreDataManager alloc] init];
    

If you want to load the Chat SDK login when your app runs you will also need to add the following lines:

        // This is the main view that contains the tab bar
    UIViewController * mainViewController = [[BAppTabBarController alloc] initWithNibName:Nil bundle:Nil];
    
    // Set the root view controller
    [self.window setRootViewController:mainViewController];

This code will automatically set up all the functionality required for the Chat SDK when the app starts. Finally make sure the following functions are either copied into your AppDelegate.m file or the code inside them is inside your functions:

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    [FBSDKAppEvents activateApp];
}

// During the Facebook login flow, your app passes control to the Facebook iOS app or Facebook in a mobile browser.
// After authentication, your app will be called back with the session information.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    if ([[url scheme] isEqualToString:[NSString stringWithFormat:@"fb%@",[BSettingsManager facebookAppId]]]) {
        return [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
    }
    else {
        return [[GIDSignIn sharedInstance] handleURL:url sourceApplication:sourceApplication annotation:annotation];
    }
}

This code is linked to social media login for the ChatSDK.

Great! You have now added Chat SDK to your project. If you load it up the chat will load but won't work properly. This is because you still need to set up your own Firebase account - Firebase links directly to the bundleID of your app.

To configure Firebase to your project you should read through the following tutorial.