LiveChat的iOS聊天小部件
LiveChat的iOS聊天小部件允许你将LiveChat集成到iOS应用中。
要求
- iOS 11.0+
- Xcode 10.0+
安装
Carthage
如果你使用Carthage来管理你的依赖项,只需在Cartfile
中将'livechat/chat-window-ios'添加上去。
github "livechat/chat-window-ios" ~> 2.0.24
确保你已经将LiveChat.framework
添加到目标中的"链接框架和库"部分,并且已经将其包含在你的Carthage框架复制构建阶段中。
CocoaPods
如果您使用CocoaPods管理依赖项,只需将LiveChat添加到您的Podfile
中即可。
pod 'LiveChat', '~> 2.0.24'
手动安装
您可以直接将iOS聊天组件集成到您的项目中,而无需使用依赖管理器。
Swift
只需将LiveChat/Sources
目录下的所有文件拖拽到您的项目中即可。
Objective-C
将LiveChat/Sources
目录下的所有文件拖拽到您的项目中。在Objective-C项目中添加第一个*.swift
文件时,Xcode会提示您创建Bridging Header。聊天组件工作不需要此,除非您计划从Objective-C调用Swift代码,否则您可以选择拒绝。有关Bridging Header和Swift与Objective-C互操作性的更多信息,请参阅此处。您需要在.m文件开头放置以下导入语句:#import "<Your Project Name>-Swift.h"
。
此外,对于Objective-C项目,您需要在项目设置中设置Embedded Content Contains Swift Code
标志为是
(在“构建设置”标签中的“构建选项”下找到)。
用法
初始化
import LiveChat
LiveChat.licenseId = "YOUR_LICENSE_ID"
默认聊天小部件演示
LiveChat.presentChat()
在客户应用视图层次结构中演示聊天小部件
您还可以接管您应用中的小部件演示。要这样做,您必须将customPresentationStyleEnabled
标志设置为true
。此标志将禁用默认小部件的演示行为,并将该逻辑留给您。现在您可以通过访问chatViewController
属性来自定义小部件的演示方式。
当customPresentationStyleEnabled
设置为false
时,则chatViewController
的值为nil
。
class YOUR_CLASS_NAME : UIViewControler, LiveChatDelegate { // Your class need to implement LiveChatDelegate protocol
@IBAction func openChat(_ sender: Any) {
LiveChat.delegate = self
LiveChat.customPresentationStyleEnabled = true
present(LiveChat.chatViewController!, animated: true) {
print("Presentation completed")
}
}
func chatDismissed() {
LiveChat.chatViewController!.dismiss(animated: true) {
print("Presentation dismissed")
}
}
}
使用UIWindowSceneDelegate
如果您的应用使用UIWindowScene API,则需要在您的窗口场景代理类中执行额外的配置步骤。
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
LiveChat.windowScene = (scene as? UIWindowScene)
}
}
设置自定义变量
如果知道客户名称或电子邮件地址,可以提供,因此客户不需要填写聊天前的调查问卷
LiveChat.name = "iOS Widget Example"
LiveChat.email = "[email protected]"
如果您想将一些额外信息与您的客户关联起来,您可以设置自定义变量
LiveChat.setVariable(withKey:"Variable name", value:"Some value")
将聊天分配到指定分组
您可以通过提供 groupId 来将您的客户路由到特定的客服团队。更多详情请参阅此处:https://www.livechatinc.com/kb/dividing-live-chat-by-group/。
LiveChat.groupId = "77"
通知用户关于客服响应
如果用户最小化了聊天窗口,您可以通过通知用户关于客服的响应。为了处理传入的消息,您的类必须实现 LiveChatDelegate
协议并将自身设置为 LiveChat.delegate
。
class YOUR_CLASS_NAME : LiveChatDelegate { // Your class need to implement LiveChatDelegate protocol
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
LiveChat.licenseId = "YOUR_LICENSE_ID"
LiveChat.delegate = self // Set self as delegate
return true
}
func received(message: LiveChatMessage) {
print("Received message: \(message.text)")
// Handle message here
}
}
示例消息结构。
{
author = {
name = "Support Bot";
};
id = "QZ0X4O6PAV_3";
messageType = newMessage;
text = "I'm a HelpDesk Bot, here to assist you with any HelpDesk questions!";
timestamp = 1632478822776;
}
处理聊天窗口存在事件
在 SDK 级别,也可以处理聊天窗口的存在事件。为此,您的类必须实现 LiveChatDelegate
协议并将自身设置为 LiveChat.delegate
。
class YOUR_CLASS_NAME : LiveChatDelegate { // Your class need to implement LiveChatDelegate protocol
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
LiveChat.licenseId = "YOUR_LICENSE_ID"
LiveChat.delegate = self // Set self as delegate
return true
}
func chatPresented() {
print("Chat presented")
// Handle event here
}
func chatDismissed() {
print("Chat dismissed")
// Handle event here
}
}
处理 URL
默认情况下,聊天消息中的所有链接都在 Safari 浏览器中打开。要更改此行为,您可以使用 LiveChatDelegate
来处理 URL。
func handle(URL: URL) {
print("URL is \(URL.absoluteString)")
// Handle URL here
}
处理聊天窗口错误
SDK 将使用此方法来报告未处理的 Widget 错误。
func loadingDidFail(with errror: Error) {
print("Chat loading failure \(errror)")
// Handle error here
}
从设备库发送文件
如果您已启用访客的文件共享,应在您的Info.plist
文件中包含NSPhotoLibraryUsageDescription
(隐私 - 照片库使用说明
)、NSCameraUsageDescription
(隐私 - 摄像头使用说明
)和NSMicrophoneUsageDescription
(隐私 - 麦克风使用说明
)键,以避免iOS 10或更高版本上的崩溃。您可以在示例项目中检查Info.plist
文件。
第三方集成
Snap Call
LiveChat SDK内建了Snap Call(《https://snapcall.io》)集成。要利用该功能,您需要准备应用程序请求用户麦克风权限。这可以通过在Info.plist
文件中添加NSMicrophoneUsageDescription
和NSCameraUsageDescription
键来完成。
示例应用
Swift和Objective-C的示例应用程序可以在Examples
文件夹中找到。
获取帮助
如果您有任何问题或想提供反馈,与我们聊天吧!
许可
iOS聊天小部件采用MIT许可。更多信息请参阅LICENSE文件。