To add Chat to your iOS app, create an SCSChatConfiguration object and pass it to the showChat method.
Before You Begin
The legacy chat product is scheduled for retirement on February 14, 2026, and is in maintenance mode until then. During this phase, you can continue to use chat, but we no longer recommend that you implement new chat channels. To avoid service interruptions to your customers, migrate to Messaging for In-App and Web before that date. Messaging offers many of the chat features that you love plus asynchronous conversations that can be picked back up at any time. Learn about chat retirement in Help.
Important
Before running through these steps, be sure you’ve already:
Import the SDK. Wherever you intend to use the Chat SDK, be sure to import the ServiceCore framework and the ServiceChat framework.
In Swift:
1import ServiceCore2import ServiceChat
In Objective-C:
1@import ServiceCore;2@import ServiceChat;
Create an SCSChatConfiguration instance with information about your LiveAgent pod, your Salesforce org ID, the deployment ID, and the button ID.
In Swift:
1let config = SCSChatConfiguration(liveAgentPod: "TO_DO_POD_NAME",2 // e.g. "d.gla5.gus.salesforce.com"3 orgId: "TO_DO_ORG_ID",4 // e.g. "00DB00000003Rxz"5 deploymentId: "TO_DO_DEPLOYMENT_ID",6 // e.g. "573B00000005KXz"7 buttonId: "TO_DO_BUTTON_ID")8 // e.g. "575C00000004h3m"
In Objective-C:
1SCSChatConfiguration *config =2[[SCSChatConfiguration alloc]initWithLiveAgentPod:@"TO_DO_POD_NAME"3 // e.g. "d.gla5.gus.salesforce.com"4 orgId:@"TO_DO_ORG_ID"5 // e.g. "00DB00000003Rxz"6 deploymentId:@"TO_DO_DEPLOYMENT_ID"7 // e.g. "573B00000005KXz"8 buttonId:@"TO_DO_BUTTON_ID"];9 // e.g. "575C00000004h3m"
You can specify both optional and required fields shown to the user before a chat session starts. You can also directly pass data to an agent without requiring any user input. These fields can be mapped directly to fields in a record in your org.
The showChat method must be called on the main UI thread.
Note
You can provide an optional completion block to execute code when the session has been fully connected to all services. During a successful session initialization, the SDK calls the completion block at the point that the session is active and the user is waiting for an agent to join. If there is a failure, the SDK calls the completion block with the associated error.
Listen for events and handle error conditions.
You can detect when a session ends by implementing the session(didEnd:) method on the SCSChatSessionDelegate delegate. Register this delegate using the add(delegate:) method on your SCSChat instance. In particular, we suggest that you handle the .agent reason (for when an agent ends a session) and the .noAgentsAvailable reason (for when there are no agents available). See Listen for State Changes and Events.
The SDK doesn’t show an alert when a session fails to start, or when a session ends. It’s your responsibility to listen to events and display an error when appropriate.
Note
These steps embed the chat experience into your app.
Use the storyboard to add a button to the view. Add a Touch Up Inside action in your UIViewController implementation with the name startChat. In the view controller code:
Implement the SCSChatSessionDelegate protocol so that you can be notified when there are errors or state changes.
Specify self as a chat delegate.
Start a chat session in the button action.
Implement the session(didEnd:) method and show a dialog when appropriate.
In Swift:
1import UIKit2import ServiceCore3import ServiceChat45class ViewController : UIViewController, SCSChatSessionDelegate {67 override func viewDidLoad(){8 super.viewDidLoad()910 // Add our chat delegate11 ServiceCloud.shared().chatCore.add(delegate: self)12}1314 @IBAction func startChat(_ sender: AnyObject){1516 // Create config object17 if let config = SCSChatConfiguration(liveAgentPod: "YOUR-POD-NAME",18 orgId: "YOUR-ORG-ID",19 deploymentId: "YOUR-DEPLOYMENT-ID",20 buttonId: "YOUR-BUTTON-ID"){2122 // Start a session23 ServiceCloud.shared().chatUI.showChat(with: config)24}25}2627 func session(_ session: SCSChatSession!, didEnd endEvent: SCSChatSessionEndEvent!){2829 var description = ""3031 // Here we'll handle the situation where the agent ends the session32 // and when there are no agents available...33 switch endEvent.reason {34 case .agent:35 description = "The agent has ended the session."36 case .noAgentsAvailable:37 description = "It looks like there are no agents available. Try again later."38 // TO DO: Handle other reasons39 default:40 description = "Session ended for an unknown reason."41}4243 let alert = UIAlertController(title: "Session Ended",44 message: description,45 preferredStyle: .alert)46 let okAction = UIAlertAction(title: "OK",47 style: .default,48 handler: nil)49 alert.addAction(okAction)50 self.present(alert, animated: true, completion: nil)51}5253 func session(_ session: SCSChatSession!, didError error: Error!, fatal: Bool){54 // TO DO: Handle error condition55 NSLog("Chat error: \(error.localizedDescription)")56}57}
In Objective-C:
1#import "ViewController.h"2@import ServiceCore;3@import ServiceChat;45@interface ViewController : UIViewController<SCSChatSessionDelegate>67@end89@implementation ViewController1011- (void)viewDidLoad {12[super viewDidLoad];1314 // Add our chat delegate15[[SCServiceCloud sharedInstance].chatCore addDelegate:self];16}1718- (IBAction)startChat:(id)sender {1920 // Create config object21 SCSChatConfiguration *config =22[[SCSChatConfiguration alloc]initWithLiveAgentPod:@"YOUR-POD-NAME"23 orgId:@"YOUR-ORG-ID"24 deploymentId:@"YOUR-DEPLOYMENT-ID"25 buttonId:@"YOUR-BUTTON-ID"];26 // Start the session27[[SCServiceCloud sharedInstance].chatUI showChatWithConfiguration:config];28}2930- (void)session:(id<SCSChatSession>)session didEnd:(SCSChatSessionEndEvent *)endEvent {3132 NSString *description = nil;3334 // Here we'll handle the situation where the agent ends the session35 // and when there are no agents available...36 switch(endEvent.reason){37 case SCSChatEndReasonAgent:38 description = @"The agent has ended the session.";39 break;40 case SCSChatEndReasonNoAgentsAvailable:41 description = @"It looks like there are no agents available. Try again later.";42 break;43 // TO DO: Handle other reasons44 default:45 description = @"Session ended for an unknown reason.";46 break;47}4849 UIAlertController *alert = [UIAlertController50 alertControllerWithTitle:@"Session Ended"51 message:description52 preferredStyle:UIAlertControllerStyleAlert];5354 UIAlertAction* okAction = [UIAlertAction55 actionWithTitle:@"OK"56 style:UIAlertActionStyleDefault57 handler:nil];5859[alert addAction:okAction];60[self presentViewController:alert animated:YES completion:nil];61}6263- (void)session:(id<SCSChatSession>)session didError:(NSError *)error fatal:(BOOL)fatal {64 // TO DO: Handle error condition65 NSLog(@"Chat error: \(error.localizedDescription)");66}6768@end