Use the SDK with Objective-C

Learn how to use the Chat SDK in an Objective-C project.

You can add a Swift file in Xcode as a translation layer to use the Kustomer Chat SDK Swift APIs in an Objective-C project.

In the translation layer Swift file, use the @objc keyword to expose the available classes and methods to the Objective-C project.

πŸ“˜

Do I need to add a bridging header file?

No. While Xcode prompts you to add a bridging header file when you add your first Swift file to an Object-C project, a bridging header file is unnecessary to expose the Swift APIs to your Objective-C code.

To learn more, see Importing Swift into Objective-C in the Xcode documentation.

Translation Layer implementation

We've provided a sample translation layer Swift file below.

import UIKit
import KustomerChat

@objc public class TranslationLayer: NSObject {
  @objc class func config(withLaunchOptions launchOptions: NSDictionary) {
    let options = KustomerOptions()
  	options.language = .fr
  	options.businessScheduleId = "1234"
  	options.hideNewConversationButtonInClosedChat = true
    Kustomer.configure(apiKey: "api key", options: nil, launchOptions: launchOptions as? [UIApplication.LaunchOptionsKey : Any])
  }
}

Once you have your translation layer, import the project file with your Objective-C project code and corresponding Swift file into your AppDelegate.m file to implement the translation layer.

We've included a code sample below.

πŸ“˜

Importing your Objective-C project code and translation later into AppDelegate.m

#import "YourProjectName-Swift.h" imports the Objective-C project that contains your Objective-C project code and the translation layer Swift file. Replace YourProjectName with the name of the relevant project.

#import "AppDelegate.h"
  
// Replace "YourProjectName" with the name of the Objective-C project
#import "YourProjectName-Swift.h"

@interface AppDelegate ()
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Override point for customization after application launch.
  [TranslationLayer configWithLaunchOptions:launchOptions];
  return YES;
}