NowChatServiceDelegate protocol - iOS

  • Release version: Yokohama
  • Updated January 30, 2025
  • 1 minute to read
  • The NowChatServiceDelegate protocol provides callbacks for notification of actions within the NowChatService such as a request to open a URL or dismiss the chat view controller.

    NowChatServiceDelegate - chatService(_chatService: NowChatService, didRequestOpenUrlurl: URL)

    Callback that notifies the host application that the specified chat service received a request to open a URL. This callback is usually triggered by a user tapping on a link in the chat UI. It is the host application's responsibility to handle opening the URL or ignore the request.

    Table 1. Parameters
    Name Type Description
    chatService NowChatService NowChatService object making the request.
    didRequestOpenUrl url URL URL that the service has requested to open.
    Table 2. Returns
    Type Description
    None

    The following code example shows how to call this function.

    func chatService(_ chatService: NowChatService, didRequestOpenUrl url: URL) {
      var updatedViewState = makeViewState()
      updatedViewState.urlToOpen = url
      viewState = updatedViewState
    }

    NowChatServiceDelegate - chatService(_chatService: NowChatService, systemThemeDidChange traitCollection: UITraitCollection)

    Callback that notifies the host application that the system chat theme changed.

    Table 3. Parameters
    Name Type Description
    chatService NowChatService NowChatService object making the request.
    traitCollection UITraitCollection UITraitCollection that contains the new theme.
    Table 4. Returns
    Type Description
    None

    The following code example shows how to override the systemThemeDidChange() delegate function to call the updateTheme() function to apply theme changes when the system theme changes.

    func chatService(_ chatService: any SnowChat.ChatServiceProvider, systemThemeDidChange traitCollection: UITraitCollection) {
    
      /// The corresponding updateTheme() method can be called here to change the UI theme based on System Theme
      chatService.updateTheme(theme: traitCollection.userInterfaceStyle == .dark ? DarkNowChatTheme() : LightNowChatTheme())
      print(“System Theme Did Change)
    }

    NowChatServiceDelegate - chatServiceViewControllerWasDismissed(_chatService: NowChatService)

    Callback that notifies the host application that the chat view controller was dismissed.

    Table 5. Parameters
    Name Type Description
    chatService NowChatService NowChatService object whose view controller was dismissed.
    Table 6. Returns
    Type Description
    None

    The following code example shows how to call this function.

    func chatServiceViewControllerWasDismissed(_ chatService: NowChatService) {
      resetChat()
    }

    NowChatServiceDelegate - didEndSessionWithId(sessionId: String)

    Called when the chat screen is closed and the chat session ends.

    Note:
    You must define the desired functionality for this callback by overriding the function as the default implementation simply prints the message "Chat Session ended with ID: \(sessionId)".
    Table 7. Parameters
    Name Type Description
    sessionId String Sys_id of the session that ended.

    Table: Conversation Session [sys_cs_session]

    Table 8. Returns
    Type Description
    None

    The following code example shows where to place your code to override the default functionality.

    func chatService(_ chatService: NowChatService, didEndSessionWithId sessionId: String) {
      print("Chat Session ended with ID: \(sessionId)")
    }