Implement Virtual and Live Agent chat

  • 릴리스 버전: Australia
  • 업데이트 날짜 2026년 03월 12일
  • 소요 시간: 3분
  • The Mobile SDK enables you to easily implement Virtual and Live Agent chat services within your iOS application.

    You use the NowChatService API to create the chat user interface. Once the UI is created, you must then start the chat session, and then start the chat service. Similar to other Mobile SDK feature services, the NowChatService API provides two implementations for some of its methods, including the startChat() method. One implementation returns a Combine publisher, and the other calls a completion handler with the return results.

    The following is a snippet from the sample application that shows how to initialize and start a chat UI and session.

    func makeChatScreen() -> UIViewController? {
      guard let chatService = chatService else { return nil }
      let result = chatService.makeChatUI(theme: CarrascoChatTheme(chatColors: ChatColors())) 
      switch result {
        case .success(let chatViewController):
          return chatViewController
        case .failure(let error):
          debugPrint("Chat screen creation failed with error: \(error)")
          return nil 
      } 
    }
    
    func startChat() {
      guard let chatService = chatService else {
        debugPrint("Chat service is invalid")
        viewState = makeViewState()
        return
      } 
      chatService.startChat { [weak self] result in
        if case .failure(let error) = result {
          debugPrint("Chat session initialization failed with error: \(error)")
          self?.resetChat()
        } 
      }
    }

    Before you can leverage the chat functionality within your application, you must configure Virtual Agent within your ServiceNow instance. For details, see Virtual Agent.

    Theme the chat user interface

    You can customize the colors of the Live Agent and Virtual Agent chat UI by passing a theme object in the makeChatUI() call. For a list of all of the elements that you can customize, see NowChatColoring protocol - iOS. By default, the chat UI uses the NowUIColor theme for all NowSDK UI elements. Refer to the NowChatThemeable protocol - iOS for sample code snippets on how to apply a theme to your chat UI.