Getting Started
The LikeMinds Chat SDK allows you to seamlessly integrate personalized and engaging chats into your iOS application. This can significantly enhance the user experience and drive user engagement within your app. This getting started guide will walk you through the steps to integrate it into your application.
Prerequisites
Before you begin, ensure that you have the following:
- Xcode: Make sure you have the Xcode 11 or above installed on your development machine.
- LikeMinds API Key: Sign up on the LikeMinds dashboard and obtain an API key for your application.
Dependencies
The SDK tries to keep the list of external dependencies to a minimum, these are the dependencies currently used:
LikeMindsChatUI
LikeMindsChatCore
LikeMindsChat
Step-by-Step Integration Guide
Follow these steps to integrate the LikeMinds Chat SDK into your iOS application using CocoaPods:
Step 1 Install LikeMindsChatCore via CocoaPods:
Open your project's Podfile
and add the following lines:
platform :ios, '13.0'
target 'YourProjectName' do
use_frameworks!
pod 'LikeMindsChatCore', '~> 1.3.0'
end
Then, run pod install
from the terminal to install the required dependencies.
Step 2 - Set Up LikeMinds Chat:
In your AppDelegate.swift
file, import the LikeMindsChatCore
framework and set up the LikeMinds Chat in the application(_:willFinishLaunchingWithOptions:)
method:
import LikeMindsChatCore
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) {
LMChatCore.shared.setupChat(deviceId: deviceId)
}
Step 3 - Initialise LikeMinds Chat:
You have successfully initiated the LMChatCore. Now, When integrating the LikeMinds Chat SDK into your application, you have the option to initiate a user session using one of two approaches:
With API Key
This approach should be used when you want to manage LikeMinds authentication tokens on frontend. In this case, you provide API Key directly to LikeMinds Chat SDK, which will be used to initiate a user session by calling showChat()
method from LMChatCore.
import LikeMindsChatCore
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) {
LMChatCore.shared.showChat(apiKey: "YOUR_API_KEY", username: username, uuid: userId, completionHandler: { })
}
Replace YOUR_API_KEY
with the API key you obtained from the LikeMinds dashboard.
It is advised to initiate LikeMindsChat at the beginning of your App Cycle
Without API Key
This approach should be used when you want to manage LikeMinds authentication tokens on your backend server. In this case, you eliminate the need to expose your API Key to your client app and your backend server is responsible for calling the initiate API to obtain the accessToken
and refreshToken
which is passed to showChat()
from LMChatCore
to validate the user session.
Create a function to get
accessToken
andrefreshToken
from your backend using the initiate API.Implement the
LMChatCoreCallback
protocol in your class:
public protocol LMChatCoreCallback: AnyObject {
func onAccessTokenExpiredAndRefreshed(accessToken: String, refreshToken: String)
func onRefreshTokenExpired(_ completionHandler: (((accessToken: String, refreshToken: String)) -> Void)?)
}
Example
class ClientSDKCallBack: LMChatCoreCallback {
func onAccessTokenExpiredAndRefreshed(accessToken: String, refreshToken: String) {
// Store the updated tokens
}
func onRefreshTokenExpired(_ completionHandler: (((accessToken: String, refreshToken: String)?) -> Void)?) {
// Call inititate api from your backend and return the fetched accessToken and refreshToken
// completionHandler?((accessToken, refreshToken))
// In case the call fails, return nil
// completionHandler?(nil)
}
}
This protocol has two methods:
onAccessTokenExpiredAndRefreshed
: Called when the access token expires and is refreshed internally.onRefreshTokenExpired
: Called when the refresh token expires. You need to provide new tokens from your backend.
- Upon receiving the
accessToken
andrefreshToken
from your backend server, callLMChatCore.showChat()
function with these tokens.
import LikeMindsChatCore
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) {
// Pass the earlier created Implementation of LMChatCoreCallback
LikeMindsChatCore.showChat(accessToken: accessToken, refreshToken: refreshToken, handler: ClientSDKCallback(), completionHandler: { })
}
Step 4 - Navigation:
In the view controller where you want to display the LikeMinds Chat, import the LikeMindsChatCore
framework and initialize the Chat with the appropriate user credentials:
import LikeMindsChatCore
let username = "USER_NAME"
let uuid = "USER_UUID"
try? LMChatCore.shared.showChat(apiKey: apiKey, username: username, userId: userId) {[weak self] result in
switch result {
case .success:
guard let viewController = try? LMChatHomeChatViewModel.createModule() else {
let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
alert.addAction(.init(title: "OK", style: .default))
self?.present(alert, animated: true)
return
}
let navigationController = UINavigationController(rootViewController: viewController)
UIApplication.shared.windows.first?.rootViewController = navigationController
UIApplication.shared.windows.first?.makeKeyAndVisible()
case .failure(let error):
// Show error message
let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
alert.addAction(.init(title: "OK", style: .default))
self?.present(alert, animated: true)
return
}
}
Replace USER_NAME
, USER_UUID
and DEVICE_ID
with the appropriate user credentials and device unique identifier for your application.
The createModule()
method returns the view controller that displays the LikeMinds Chat. - In case of success, it sets the returned view controller as the root view controller of the application's window. - In case of failure, it displays an alert with the error message.
The view controller where you initialize the LikeMinds Chat should have a navigation controller, as the Chat requires navigation capabilities.
That's it! You have successfully integrated the LikeMinds Chat SDK into your iOS application. The next step would be to explore additional customization options or configurations provided by the SDK to tailor the Chat to your application's needs.