Get Started - iOS

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

Quick Start

TapTalk.io helps you to implement real-time chat with any type of your client app with speed and efficiency. Our iOS SDK provides you with various methods to initialize, configure, and build the chat from the client-side - no server-side implementation is required because our reliable infra management service is delivered with the SDK. This page presents a brief overview of the SDK’s structure and abilities, then lets you go through the preliminary steps of implementing the SDK in your own app. We will be using Xcode for the native side implementation.

Configure and Initialize TapTalk.io iOS SDK

Step 1: Create a new application from your dashboard

1. Login to TapTalk.io Dashboard, then choose Development -> Apps

2. Click New App Button, input App Name and choose Platform, and then click Create New App Button.

3. A pop-up dialog will be shown with provided App Key ID & App Key Secret

Note: Please remember to save your App Key ID & your App Key Secret because it will only be shown once and will be used in TapTalk.io initialization

Step 2: Install TapTalk.io SDK

You can install the PowerTalk 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 'TapTalk'
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.

Privacy - Microphone Usage Description

$(PRODUCT_NAME) needs to request access to your microphone to record audio to send voice 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, we can 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. openPowerTalkUI() function to open the chat room list view

  3. A button to call the openPowerTalkUI() function

main.dart
class _MyHomePageState extends State<MyHomePage> {
  
  // Create the MethodChannel
  static const powerTalkMethodChannel = MethodChannel('io.taptalk.powertalk');
  
  // Function to open the chat room list UI
  Future<void> _openPowerTalkUI() async {
    try {
      // Initialize PowerTalk SDK
      final bool isPowerTalkInitialized = await powerTalkMethodChannel.invokeMethod('initPowerTalk');
      if (isPowerTalkInitialized) {
        // Authenticate PowerTalk SDK
        final bool isPowerTalkAuthenticated = await powerTalkMethodChannel.invokeMethod('authPowerTalk');
        if (isPowerTalkAuthenticated) {
          // Open chat room list
          await powerTalkMethodChannel.invokeMethod('openChatRoomList');
        }
      }
    }
    on PlatformException catch (e) {
      print(e.message);
    }
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      ...
      body:
        ...
          // Sample button to call openPowerTalkUI
          ElevatedButton(
            onPressed: _openPowerTalkUI,
            child: Text(
              'Get Started with PowerTalk',
            ),
          ),
      ...
    );
  }
}

Please note the name parameter in the MethodChannel constructor that was used (io.taptalk.powertalk), and the method name in the invokeMethod() parameter (initPowerTalk, authPowerTalk & openChatRoomList). 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 PowerTalk SDK and open the chat room list view

AppDelegate.m
// Import TapTalk.io
#import <TapTalk/TapTalk.h>

@interface AppDelegate () <TapTalkDelegate>

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

@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 *powerTalkMethodChannel = [FlutterMethodChannel methodChannelWithName:@"io.taptalk.powertalk"
                                                                               binaryMessenger:controller.binaryMessenger];

    // Handle MethodChannel calls
    [powerTalkMethodChannel setMethodCallHandler:^(FlutterMethodCall *call, FlutterResult result) {
        // Save result to variable
        self->_powerTalkMethodChannelResult = result;
        
        // Call native methods
        if ([call.method isEqualToString:@"initPowerTalk"]) {
            [self initPowertalk];
        }
        else if ([call.method isEqualToString:@"authPowerTalk"]) {
            [self authenticatePowertalk];
        }
        else if ([call.method isEqualToString:@"openChatRoomList"]) {
            [self openChatRoomList];
        }
        else {
            result(FlutterMethodNotImplemented);
        }
    }];
    
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

// Method to initialize PowerTalk SDK
- (void)initPowertalk {
    [[TapTalk sharedInstance] initWithAppKeyID:POWERTALK_APP_KEY_ID
                                  appKeySecret:POWERTALK_APP_KEY_SECRET
                                  apiURLString:POWERTALK_APP_BASE_URL
                            implementationType:TapTalkImplentationTypeCombine];
    
    if (self.powerTalkMethodChannelResult != nil) {
        self.powerTalkMethodChannelResult(@(YES));
    }
}

// Method to authenticate PowerTalk SDK
- (void)authenticatePowertalk {
    [[TapTalk sharedInstance] authenticateWithAuthTicket:ticket
                                      connectWhenSuccess:YES
    success:^{
        if (self.powerTalkMethodChannelResult != nil) {
            self.powerTalkMethodChannelResult(@(YES));
        }
    }
    failure:^(NSError * _Nonnull error) {
        if (self.powerTalkMethodChannelResult != nil) {
            self.powerTalkMethodChannelResult([FlutterError errorWithCode:[@(error.code) stringValue]
                                                                  message:error.localizedDescription
                                                                  details:nil]);
        }
    }];
}

// Method to open chat room list view
- (void)openChatRoomList {
    TapUIRoomListViewController *roomListViewController = [[TapUI sharedInstance] roomListViewController];
    UINavigationController *roomListNavigationController = [[UINavigationController alloc] initWithRootViewController:roomListViewController];
    roomListNavigationController.modalPresentationStyle = UIModalPresentationOverFullScreen;
    
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    [rootViewController presentViewController:roomListNavigationController animated:YES completion:^{
        if (self.powerTalkMethodChannelResult != nil) {
            self.powerTalkMethodChannelResult(@(YES));
        }
    }];
}

You can obtain POWERTALK_AUTH_TICKET by following the guide in Authentication page.

Step 8: Implement 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. You can follow the instructions in Implement Application Delegate page.

Note: In the Event Delegate page, you can find detailed information on the usages of TapTalk iOS SDK's delegates and callbacks.

You can now try running the app. Pressing the sample button will invoke the openPowerTalkUI method that we previously created, which will initialize & authenticate PowerTalk SDK, and open the chat room list view once the initialization is completed. You may also try a different implementation and separate the init, authenticate, 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 PowerTalk iOS section.

Last updated