ChatProvider Class reference for Kustomer Chat iOS Core API .

public class ChatProvider


public static let shared: ChatProvider

The central object for managing non-UI Kustomer chat related activities.



public var listeners: [String : KUSChatListener]

All currently registered KUSChatListeners.


public func addChatListener(_ listener: KUSChatListener) -> String

Register a KusChatListener to the Core SDK to receive chat events


public func removeChatListener(_ uuid: String)

Unregister a KUSChatListener using its unique String identifier


public func removeAllChatListeners()

Removes all registered KUSChatListeners from the Core SDK


public func listenForTyping(conversationId: String)

Begins listening for typing events.


public func stopListeningForTyping(conversationId: String)

Reading data


public func getConversations() -> [KUSConversation]

All conversations for the current customer.

Immediately returns last known list of conversations for the current customer from the SDK’s persistent offline storage.


public func getConversation(conversationId: String) -> KUSConversation?

Returns a KUSConversation object for the given id. Returns nil if the ID is not found.


public func chatMessages(conversationId: String) -> [KUSChatMessage]

Immediately returns last known list of messages for a conversation. Use a KUSChatListener to be notified when new messages come in.

getChatMessages(conversationId:, completion:)

public func getChatMessages(conversationId:String, completion: @escaping (Result<[KUSChatMessage],KError>) -> Void)

Gets the historical messages for a conversation using a network call to Kustomer's API (up to 30 days)


public func unreadCount() -> Int

Immediately returns the current customer’s unread count. Use a KUSChatListener to be notified when this changes.


public func openConversationCount() -> Int


Deprecated. Use customerExists() instead.

public func currentCustomer() -> KUSCustomer?

Information about the current customer. Value is persisted across application restarts.

Is nil if you have not successfully called logIn(...) and there are no anonymous chats.

Anonymous chats are chats created by users who have not called logIn(...).

After logOut(...) this becomes nil.


public func customerExists() -> Bool

Returns true if there is a current Customer (either anonymous or logged-in) in the Kustomer system.


public func getChatSettings() -> KUSChatSettings

Returns chat settings. Does not attempt to load or update them from the REST API


public func isChatAvailable(_ callback: ((Bool) -> Void))

Check the “turned on/off” status of your chat within Business Hours and outside of Holidays settings asynchronously. For example, if chat is turned off or outside of business hours, you may want to turn off the button or deflect customers to contact an email).

If you change your business hours or holidays in the admin panel, you must restart the app to see the new values.

Writing data


 public func createConversation(title text: String,
                                   assistant: KUSAssistant? = nil,
                                   completion: @escaping ((Result<(conversation: KUSConversation, messages: [KUSChatMessage], customer: KUSCustomer), KError>) -> Void))

Creates a conversation with a message.

Important: the KUSChatMessages returned in messages may or may not have been sent successfully yet. check their .status. and use the observer on new message create to detect when they are.



Reply to a message from a chat assistant using an action (action == a quick reply button)

Note: The conversation ID and message IDs are automatically tracked by the SDK. You only need to pass a [KUSMessageAction](../Structs/KUSMessageAction.html) from the KUSMessage that you’re replying to.

public func sendChatMessage(action: KUSMessageAction, completion _outerCompletion: @escaping (KUSChatMessage, KUSConversation) -> Void)


Sends a text message as a reply to a chat assistant question.

public func sendChatMessage(text: String, assistant: KUSAssistant, completion _outerCompletion: @escaping (KUSChatMessage, KUSConversation) -> Void)

Note: For non-conversational-assistant conversations, use sendChatMessage(text:String, conversationId:String, completion: @escaping ((KError?) -> Void)) instead.


Reply to a message from a chat assistant using an MLL node

public func sendChatMessage(mllNode: KUSMLLNode, completion _outerCompletion: @escaping (KUSChatMessage, KUSConversation) -> Void)

Note: The conversation ID and message IDs are automatically tracked by the SDK. You only need to pass a KUSMLLNode from the KUSMessage that you’re replying to.


Posts a message containing an image to a conversation.

public func sendChatMessage(image:UIImage,
                                completion: @escaping ((KError?) -> Void))


Posts a message containing a file or video to a conversation.

public func sendChatMessage(fileUrl:URL,
                                completion: @escaping ((KError?) -> Void))


Creates a new KUSAssistant. Fetches the properties needed to use this assistant. Gets a list of initialMessages that are the same format as KUSChatMessage. A conversation (KUSConversation) on our system (that shows up on the Kustomer website) is not created. A KUSConversation is only created after you submit a message using this assistant.

1.  When a conversation is created, you will get notifications for each message in `initialMessage`s, but they will have different `id`s and potentially `createdAt` dates.

2.  `initialMessages` is a template for what you should show your customer. They aren’t actual `KUSChatMessages` that are on our platform.
 public func createAssistant(id: String,
                                after: @escaping ((KUSAssistant) -> Void),
                                error: @escaping ((KError) -> Void),
                                afterCreateConversation: ((KUSConversation)->Void)? = { _ in }) -> String


Ends an existing conversation.

public func endConversation(conversationId: String, completion: @escaping ((Result<KUSConversation, KError>) -> Void))


Reloads chat settings and business schedules from our REST API. If the network call fails, returns an error.

public func reloadChatSettings(_ callback: @escaping ((KError?) -> Void))


Attach custom attributes to the user’s most recent conversation (or the first one they create).

See Describe Conversation.

public func describeConversation(conversationId: String, attributes: [String : Any], _ completion: @escaping ((Result<Void, KError>) -> Void))

NOTE: These key-value pairs must be enabled on the Conversation Klass via the admin portal. This can be done by an admin via Settings > Platform Settings > Klasses > Conversation

- attributes: A dictionary with the custom information for the Session. You can pass this any key/value pair and the backend will add that information as Custom fields. The type name is appended to the end of the custom field. So if you see a custom string field on your admin dashboard called `favoriteColor`, you should pass `favoriteColorStr` to this method. Suffixes are: `Num` for numbers, `Tree` for multi-level lists, `Bool` for true/false, `Url` for urls, `Str` for strings.


`self.describe(attributes: ["aNumberNum": 10, "aMlListTree": "scotland", "aDateAt":Date(), "aTrueFalseBool": true, "aUrlUrl":"", "favoriteColorStr":"Blue"]) { result in switch result { case .success: kprint("Ok") case .failure(let error): kprint(error.localizedDescription) if case let .describeUnknownParameter(parameter: p, details: details) = error { kprint("unknown param is \(p)") kprint(details!) } if case let .describeParameterWrongType(parameter: p, details: details) = error { kprint("wrong type param is \(p)") kprint(details!) } } }`

describeCurrentCustomer(phone: email:phones:emails:facebook:instagram:twitter:linkedIn:custom:_:)

Attach custom attributes to the customer.

See Describe Customer.

public func describeCurrentCustomer(phone: String? = nil, email: String? = nil, phones: [String]? = nil, emails: [String]? = nil, facebook: String? = nil, instagram: String? = nil, twitter: String? = nil, linkedIn: String? = nil, custom: [String : Any]? = nil, _ completion: ((Result<Void, KError>) -> Void)? = nil)

Note: Attached key-value pairs via the custom property must be enabled on the Customer Klass via the admin portal. This can be done by an admin via Settings > Platform Settings > Klasses > Customer, and works the same way as describeConversation


Marks a conversation as read as of right now. Any message received for this conversation after this will be treated as unread, until marked read again. This function should be called, whenever the user closes the chat view for a conversation.

public func markRead(conversationId: String)


See Log in and authentication.


Deprecated. See Log in and authentication.


public func willCrash()

Did this page help you?