Get Started - iOS

This section will cover a step-by-step approach to bridge your Flutter project to OneTalk Live Chat iOS SDK. We will be using Xcode to configure native code in Flutter project.

Before you get started: Make sure you have created a channel in OneTalk dashboard and obtain the APP_KEY_SECRET.

Configure and Initialize TapTalk.io Omnichannel iOS SDK for Flutter project

Step 1: Install TapTalk.io Omnichannel iOS SDK with CocoaPods

You can install the TapTalk.io Omnichannel iOS SDK using CocoaPods like the following. If you are new to CocoaPods, you can see more information in Cocoapods guides. First, navigate to the ios folder of your Flutter project directory, and check if a file named Podfile exists. If there is no Podfile in the ios folder, we will create a new one using terminal. Open a terminal window and navigate to the ios directory of your Flutter project, then create a Podfile by running the following command in the terminal:

$ pod init

Then Podfile will be created in your project directory. Open the Podfile and add the following lines to the Podfile.

# If you specified a platform for your project,
# please make sure to use ios 11.0 for minimum deployment target
platform :ios, '11.0'
use_frameworks!

target "YourProjectName" do
    pod 'TapTalkLive'
end

Since TapTalk.io use uses git-lfs (Git Large Files Storage) you will need to install GIT LFS to clone/install TapTalk.io SDK through Cocoapods.

Note: Make sure to install git-lfsbefore pod install, otherwise the pod install / pod update will return an error

Easiest way to install git-lfs is using brew:

brew install git-lfs
git lfs install

Next, after the git-lfs is installed, install the TapTalk.io SDK through CocoaPods.

$ pod install

Step 3: Include Pods Runner in xcconfig

After the pod is successfully installed, we will handle the implementation in the native side using Xcode. Open the xcworkspace file in your project's ios folder. (The default project name for iOS should be Runner, in this case, open Runner.xcworkspace).

If you previously created a new Podfile in the ios folder, open Debug.xcconfig file under the Flutter folder (you can also press Cmd + Shift + O to search by file name) and add the following:

#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"

Then open the Release.xcconfig file and add the following lines:

#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"

Step 4: Grant permission in your application project

To make sure TapTalk.io has all permission to access the user's media, file, location, and contact, the application needs to ask for permission. Open the info.plist file under the Runner folder, and add the following key-value pairs.

Information Property Key

Information Property Value

Privacy - Camera Usage Description

$(PRODUCT_NAME) needs to request access to your camera to send image messages and take profile picture.

Privacy - Contacts Usage Description

$(PRODUCT_NAME) need your permission to access your contact, we will sync your contact to our server and automatically find your friend so it is easier for you to find your friends.

Privacy - Photo Library Additions Usage Description

$(PRODUCT_NAME) needs to request access to your photo library to save photos.

Privacy - Photo Library Usage Description

$(PRODUCT_NAME) needs to request access to your photo library to send image messages and select profile picture.

Privacy - Location When In Use Usage Description

$(PRODUCT_NAME) needs to request access to your location to send location messages.

Note: You can change Information Property Value with your preferred message, the string value will be displayed when the application requests the permission.

Step 5: Enable background modes

Background modes is required to handle background process in TapTalk.io. It is used to run some processes in the background before the app is killed. For more information about the processes, check out the background process section. To enable background modes, go to Target -> Capabilities, then turn Background Modes toggle to ON, next select Background fetch and Remote notifications

After you turn on the Background Modes, Required background modes key will be added automatically in your Info.plist as shown below.

After adding the required dependency, sync your project and begin the next step by writing custom platform-specific code using platform channels.

Step 6: Create Flutter Platform Client

If you have finished this step from the Android side, you can skip to the next step.

In this example, we will add the following inside the MyHomePageState class:

  1. Method Channel to connect to the native iOS host

  2. openOneTalkLiveChatTalkUI() function to open the Live Chat's view

  3. A button to call the openOneTalkLiveChatTalkUI() function

main.dart
class _MyHomePageState extends State<MyHomePage> {
  
  // Create the MethodChannel
  static const oneTalkLiveChatMethodChannel = MethodChannel('io.taptalk.onetalklivechat');
  
  // Function to open the live chat view
  Future<void> _openOneTalkLiveChatTalkUI() async {
    try {
      // Initialize OneTalk Live Chat SDK
      final bool isOneTalkInitialized = await oneTalkLiveChatMethodChannel.invokeMethod('initOneTalkLiveChat');
      if (isOneTalkInitialized) {
        // Open live chat view
        await oneTalkLiveChatMethodChannel.invokeMethod('openLiveChatView');
      }
    }
    on PlatformException catch (e) {
      // Call might fail if the platform doesn’t support the platform API
      print(e.message);
    }
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      ...
      body:
        ...
          // Sample button to call openOneTalkLiveChatTalkUI
          ElevatedButton(
            onPressed: _openOneTalkLiveChatTalkUI,
            child: Text(
              'Get Started with OneTalk Live Chat',
            ),
          ),
      ...
    );
  }
}

Please note the name parameter in the MethodChannel constructor that was used (io.taptalk.onetalklivechat), and the method name in the invokeMethod() parameter (initOneTalkLiveChat & openLiveChatView). We will use the same names later to configure Flutter engine in the native side.

Step 7: Add an iOS platform-specific implementation

Next, we will configure the native side of the iOS host implementation. Go back to previously opened Xcode project, and open AppDelegate.m file. We will do the following:

  1. Add a MethodChannel Result variable to handle asynchronous call

  2. setMethodCallHandler to handle calls from the MethodChannel that we previously created.

  3. Add native functions to initialize & authenticate OneTalk Live Chat SDK and open the chat room list view

AppDelegate.m
// Import TapTalkLive
#import <TapTalkLive/TapTalkLive.h>

@interface AppDelegate () <TapTalkLiveDelegate>

// MethodChannel Result to handle asynchronous call
@property (nonatomic) FlutterResult oneTalkMethodChannelResult;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  
    [GeneratedPluginRegistrant registerWithRegistry:self];
  
    // Override point for customization after application launch.
    
    FlutterViewController *controller = (FlutterViewController *) self.window.rootViewController;
    
    // Create MethodChannel
    FlutterMethodChannel *oneTalkMethodChannel = [FlutterMethodChannel methodChannelWithName:@"io.taptalk.onetalklivechat"
                                                                             binaryMessenger:controller.binaryMessenger];
    // Handle MethodChannel calls
    [oneTalkMethodChannel setMethodCallHandler:^(FlutterMethodCall *call, FlutterResult result) {
        // Save result to variable
        self->_oneTalkMethodChannelResult = result;
        
        // Call native methods
        if ([call.method isEqualToString:@"initOneTalkLiveChat"]) {
            [self initOneTalkLiveChat];
        }
        else if ([call.method isEqualToString:@"openLiveChatView"]) {
            [self openLiveChatView];
        }
        else {
            result(FlutterMethodNotImplemented);
        }
    }];
    
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

// Method to initialize OneTalk Live Chat SDK
- (void)initOneTalkLiveChat {
    [[TapTalkLive sharedInstance] initWithSecretKey:ONETALK_APP_KEY_SECRET
    success:^{
        if (self.oneTalkMethodChannelResult != nil) {
            self.oneTalkMethodChannelResult(@(YES));
        }
    }
    failure:^(NSString *errorMessage) {
        if (self.oneTalkMethodChannelResult != nil) {
            self.oneTalkMethodChannelResult([FlutterError errorWithCode:YOUR_ERROR_CODE
                                                                message:errorMessage
                                                                details:nil]);
        }
    }];
}

// Method to open live chat view
- (void)openLiveChatView {
    UINavigationController *rootNavigationController = (UINavigationController *) [UIApplication sharedApplication].keyWindow.rootViewController;
    [[TapTalkLive sharedInstance] presentTapTalkLiveViewWithCurrentNavigationController:rootNavigationController animated:YES];
    if (self.oneTalkMethodChannelResult != nil) {
        self.oneTalkMethodChannelResult(@(YES));
    }
}

Step 8: Initialize TapTalk.io Omnichannel Application Delegate

To allow the iOS SDK to respond to the connection and state changes in your iOS client app, you have to implement all of our application delegate methods in your UIApplicationDelegate methods in appDelegate file.

After you initialize the TapTalk.io Omnichannel, you have to connect and implement these methods in your UIApplicationDelegate methods in appDelegate file to make sure TapTalk.io Omnichannel runs smoothly in your application.

TapTalk.io Omnichannel Application Delegate Method

Description

application:didFinishLaunchingWithOptions:

Tells the delegate that the launch process is almost done and the app is almost ready to run.

applicationWillResignActive:

Tells the delegate that the app is about to become inactive.

applicationDidEnterBackground:

Tells the delegate that the app is now in the background.

applicationWillEnterForeground:

Tells the delegate that the app is about to enter the foreground.

applicationDidBecomeActive:

Tells the delegate that the app has become active.

applicationWillTerminate:

Tells the delegate when the app is about to terminate.

handleException:

Tells the delegate when application throws exception.

AppDelegate.m
// Import TapTalkLive
#import <TapTalkLive/TapTalkLive.h>

// Implement TapTalkLiveDelegate
@interface AppDelegate () <TapTalkLiveDelegate>

// Properties
...

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  
    // Implement TapTalk Omnichannel didFinishLaunchingWithOptions here    
    [[TapTalkLive sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
    
    // Register Uncaught Exception Handler
    NSSetUncaughtExceptionHandler(&handleExceptions);
    
    // Other code
    ...
    
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

- (void)applicationWillResignActive:(UIApplication *)application {
  
    // Implement TapTalk Omnichannel applicationWillResignActive here
    [[TapTalkLive sharedInstance] applicationWillResignActive:application];
  
    // Other code
    ...
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Implement TapTalk Omnichannel applicationDidEnterBackground here
    [[TapTalkLive sharedInstance] applicationDidEnterBackground:application];
  
    // Other code
    ...
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
  
    // Implement TapTalk Omnichannel applicationWillEnterForeground here
    [[TapTalkLive sharedInstance] applicationWillEnterForeground:application];
  
    // Other code
    ...
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
  
    // Implement TapTalk Omnichannel applicationDidBecomeActive here    
    [[TapTalkLive sharedInstance] applicationDidBecomeActive:application];
  
    // Other code
    ...
}

- (void)applicationWillTerminate:(UIApplication *)application {
  
    // Implement TapTalk Omnichannel applicationWillTerminate here
    [[TapTalkLive sharedInstance] applicationWillTerminate:application];
  
    // Other code
    ...
}

// Add function to handle exceptions
- (void)handleExceptions:(NSException *exception) {
  // Implement TapTalk Omnichannel handleException method
  [[TapTalkLive sharedInstance] handleException:exception];
}

Note: Don't forget to register for exception handler inside application:didFinishLaunchingWithOptions:method and implement handleExceptions method in appDelegate class to make sure TapTalk.io Omnichannel SDK is able to handle exceptions.

You can now try running the app. Pressing the sample button will invoke the openOneTalkLiveChatTalkUI() method that we previously created, which will initialize OneTalk Live Chat SDK, and open the live chat view once the initialization is completed. You may also try a different implementation and separate the init and the open view methods. You can also add more calls to other native methods with the same approach using the same MethodChannel.

Note: You can check a more complete implementation guide in the OneTalk Live Chat for iOS section.

Last updated