This topic walks you through integrating an AI agent into an iOS application for real-time messaging.
Environment requirements
Xcode 16.0 or later. We recommend the latest official version.
CocoaPods 1.9.3 or later.
A physical device running iOS 11.0 or later.
Integrate the SDK
target 'Your Target' do
# Integrate ApsaraVideo MediaBox SDK that supports Alibaba Real-Time Communication (ARTC), such as AliVCSDK_ARTC, AliVCSDK_Standard, or AliVCSDK_InteractiveLive.
pod 'AliVCSDK_ARTC', '~> x.x.x'
# Integrate AICallKit SDK.
pod 'ARTCAICallKit/Chatbot', '~> x.x.x'
# Integrate AliVCInteractionMessage with a minimum version of 1.7.0
pod 'AliVCInteractionMessage', '~> x.x.x'
...
endThe minimum compatible version of the ARTC SDK is V7.1.0. Latest version: 7.8.1.
Latest version of AICallKit SDK: 2.9.0.
Latest version of AliVCInteractionMessage: 1.8.0.
Configure the project
Open the info.Plist file of your project and add the NSMicrophoneUsageDescription and NSCameraUsageDescription permissions to access the camera and microphone.
Open project settings, and enable Background Modes in Signing & Capabilities. If not enabled, the call cannot continue when the app enters the background. In this case, you need to call an API to end the call in your app.
Sample code
// Import the SDK.
import ARTCAICallKit
// Create an engine instance.
let engine: ARTCAIChatEngineInterface = {
return ARTCAICallEngineFactory.createChatEngine()
}()
// Configure callbacks.
self.engine.delegate = self
// Start a chat.
// We recommend that you use the user ID for logon.
let userId = "xxx"
// Set deviceId.
let deviceId = UIDevice.current.identifierForVendor?.uuidString
let userInfo = ARTCAIChatUserInfo(userId, deviceId)
// Set the agent. The agent ID cannot be nil.
let agentInfo = ARTCAIChatAgentInfo(agentId: "xxx")
let sessionId = "\(userInfo.userId)_\(agentInfo.agentId)"
self.engine.startChat(userInfo: userInfo, agentInfo: agentInfo, sessionId: self.sessionId)
// End the chat.
// If you have multiple chatbot agents, you do not need to log out when ending the current chat. Set needLogout to false.
self.engine.endChat(needLogout: false)
// Otherwise, set needLogout to true and call destroy to release resources.
self.engine.endChat(needLogout: true)
self.engine.destroy()
// For more information about other methods, see API reference.
// Callback handling. The sample code only shows some core callback operations.
public func onRequestAuthToken(userId: String, responseBlock: @escaping (ARTCAIChatAuthToken?, NSError?) -> Void) {
// Request an AuthToken. This is triggered when ChatEngine needs to log on to IM or when the IM logon AuthToken expires.
self.fetchAuthToken(userId: userId) { authToken, error in
responseBlock(authToken, error)
}
}
public func onEngineStateChange(state: ARTCAIChatEngineState) {
// The agent connection status changes.
}
public func onErrorOccurs(error: NSError, requestId: String?) {
// If requestId is not empty, an error occurred during message processing.
if let _ = requestId {
if error.aicall_code == .ChatTextMessageReceiveFailed {
// Handle text message error.
return
}
if error.aicall_code == .ChatVoiceMessageReceiveFailed {
// Handle voice message error.
return
}
if error.aicall_code == .ChatPlayMessageReceiveFailed {
// Handle playback error.
return
}
return
}
// If requestId is empty, an engine error occurred.
if let code = error.aicall_code {
if code == .TokenExpired {
// Authentication expired.
}
else if code == .AgentNotFound {
// The agent ID does not exist.
}
else if code == .KickedBySystem {
// The role is kicked out by the system, so the session was terminated.
}
else if code == .KickedByUserReplace {
// The account is being used in another session.
}
}
// Handle other errors.
}
public func onUserMessageUpdated(message: ARTCAIChatMessage) {
// The user-sent message needs to be updated.
}
public func onReceivedMessage(message: ARTCAIChatMessage) {
// Received a reply message from the agent. This callback is triggered when the agent sends a new reply or when the status of a message changes during the current reply process.
}
public func onAgentResponeStateChange(state: ARTCAIChatAgentResponseState, requestId: String?) {
// The agent response status changes.
}
public func onMessagePlayStateChange(message: ARTCAIChatMessage, state: ARTCAIChatMessagePlayState) {
// The agent message playback status changes.
}