table-ios-sdk
用于TABLE.co的iOS SDK
安装指南
-
使用CocoaPods安装
CocoaPods是一个用于Cocoa项目的依赖管理器。有关使用和安装说明,请访问它们的网站。要使用CocoaPods将TableSDK集成到您的Xcode项目中,在您的Podfile中指定它
pod 'TableSDK', '~> 0.1'
-
导入并初始化SDK
import TableSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Table.initialize(workspaceUrl: "https://YOUR_WORKSPACE.table.co", apiKey: "YOUR_SDK_API_KEY", onSuccessInitializeCompletion: {
//handle success
}) { (errorCode, errorMessage) in
//handle error
}
}
}
//...rest of your file...
您的SDK API密钥可以在您的Workspace的组织设置部分找到。
用法
在调用需要用户信息的例如Table.showConversationList()
方法之前,您需要调用Table.registerUnidentifiedUser()
或Table.registerUser()
。
注册已登录用户
var userAttributes = UserAttributes()
userAttributes.firstName = "Name"
userAttributes.lastName = "LastName"
userAttributes.email = "[email protected]"
Table.registerUser(withUserId: "USER_ID", userAttributes: userAttributes, onSuccessLoginCompletion: {
//handle success
}) { (errorCode, errorMessage) in
//handle error
}
匿名注册未知用户
Table.registerUnidentifiedUser(onSuccessLoginCompletion: {
//handle success
}) { (errorCode, errorMessage) in
//handle error
}
注销
Table.logout()
向用户显示表格会话列表
Table.showConversationList(parentViewController: self) { (errorCode, errorMessage) in
//handle error
}
推送通知
IOS推送通知
我们通过APNS使用推送通知。如何为使用推送通知设置您的项目,您可以在这里找到:[设置APNS](https://developer.apple.com/documentation/usernotifications/registering_your_app_with_apns)。
APNS令牌
要开始接收来自表格会话的iOS推送通知,您需要将上述包提供的APNS令牌传递到该函数。
Table.updateNotificationToken(token: token)
检测表格消息
为了检查你收到的推送通知是否为表格消息,你应该在UNUserNotificationCenterDelegate的函数中处理UNNotificationResponse,并在这里使用Table.showConversationList()和tableId(tableId是消息来自的会话ID)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let tableId = userInfo["table_id"] as? String {
if let rootViewController = self.window?.rootViewController as? UINavigationController {
Table.showConversationList(parentViewController: rootViewController, tableId: tableId) { (errorCode, errorMessage) in
//handle error
}
}
}
print(userInfo)
completionHandler()
}