iOS
Integrate an AI agent into your iOS application for real-time messaging by using the AICallKit SDK.
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'
...
end
The minimum compatible version of the ARTC SDK is V7.1.0. Latest version: 7.10.0.
Latest version of AICallKit SDK: 2.11.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 keys for camera and microphone access.
-
Open project settings and enable Background Modes in Signing & Capabilities. If Background Modes is not enabled, the call cannot continue when the app enters the background, and you must call an API to end the call.
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.
}