FlowCore 0.2.1

FlowCore 0.2.1

测试已测试
语种语言 SwiftSwift
许可证 MIT
发布最后发布2017年8月
SwiftSwift 版本3.0
SPM支持 SPM

Flow.ai 维护。



 
依赖关系
SwiftWebSocket~> 2.6.5
HandyJSON~> 1.6.1
 

FlowCore 0.2.1

flow.ai Swift SDK

从您的 iOS 或 macOS 应用程序访问 flow.ai 平台。这个库允许您使用 flow.ai Socket API 进行构建。

安装

flow.ai 支持 iOS 10 及以上版本。

示例应用程序

要运行示例项目,先克隆仓库,然后从 Example 目录运行 pod install

接下来打开 FlowCore.xcworkspace 文件。

使用说明

要求

您需要一个 flow.ai 账户。登录仪表板,并从 Web API 通道复制 clientId。

启动连接

在 AppDelegate 文件中导入 LiveClient。SDK 开启一个实时连接,因此您应该像对待 applicationWillTerminate(_ application: UIApplication) 事件一样关闭连接。

只需调用 start() 以打开连接并调用 stop() 以关闭它。

示例

import UIKit
import FlowCore

// Global LiveClient
let liveClient = LiveClient(

    // See the dashboard for your own clientId
    clientId: "your-client-id-from-the-dashboard",

    // Specify a threadId to make this unique for the user
    threadId: "john-doo"
)

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        liveClient.stop()
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        liveClient.stop()
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        liveClient.start()
    }

    func applicationWillTerminate(_ application: UIApplication) {
        liveClient.stop()
    }
}

接收事件

LiveClient 支持一个您可以在视图控制器中实现的代理。

public protocol LiveClientDelegate {
    func clientDidConnect(_ client:LiveClient)
    func clientWillReconnect(_ client:LiveClient)
    func clientDidDisconnect(_ client:LiveClient)
    func client(_ client:LiveClient, didReceiveReply reply: Reply)
    func client(_ client:LiveClient, didSendMessage message: Message)
    func client(_ client:LiveClient, didDeliverMessage message: Message)
    func client(_ client:LiveClient, didReceiveError error: Error)
    func client(_ client:LiveClient, didReceiveHistory history: [Reply])
}

示例

extension MessagesController : LiveClientDelegate {

    func clientDidConnect(_ client:LiveClient) {
        print("Did connect")
    }

    func clientWillReconnect(_ client:LiveClient) {
        print("Will reconnect")
    }

    func clientDidDisconnect(_ client:LiveClient) {
        print("Did disconnect")
    }

    func client(_ client:LiveClient, didReceiveReply reply: Reply) {
        print("Did receive reply")
    }

    func client(_ client:LiveClient, didSendMessage message: Message) {
        print("Did send message")
    }

    func client(_ client:LiveClient, didDeliverMessage message: Message) {
        print("Did deliver message")
    }

    func client(_ client:LiveClient, didReceiveError error: Error) {
        print("Did receive error", error)
    }

    func client(_ client:LiveClient, didReceiveHistory history: [Reply]) {
    }
}

发送消息

在启动连接后,使用 send() 方法发送消息。

示例

let speech = "I would like to order a pizza"

var originator = Originator()
originator.name = "Gijs"
originator.profile.fullName = "Gijs van de Nieuwegiessen"

let message = Message(speech: speech!, originator: originator)

liveClient.send(message)

加载历史记录

您可以使用 loadHistory() 加载以前的消息。您可以选择提供一个自定义的 threadId 以加载特定线程或用户的消息。

SDK 参考文档

阅读完整的 SDK 参考文档

作者

flow.ai

许可证

FlowCore 可在 MIT 许可下使用。有关更多信息,请参阅 LICENSE 文件。