FlowCore 0.2.1

FlowCore 0.2.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Aug 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Flow.ai.


Downloads

Total27
Week0
Month0

Installs

Apps5
powered by Segment

GitHub

Stars0
Watchers0
Forks0
Issues0
Contributors1
Pull Requests0

Code

Files30
LOCLines of Code 1,047


 
Depends on:
SwiftWebSocket~> 2.6.5
HandyJSON~> 1.6.1
 

FlowCore 0.2.1

The flow.ai Swift SDK

Access the flow.ai platform from your iOS or macOS app. This library lets you build on the flow.ai Socket API.

Installation

Flow.ai for iOS supports iOS 10 and higher.

Example app

To run the example project, clone the repo, and run pod install from the Example directory first.

Next open the FlowCore.xcworkspace file.

Usage

Requirements

You'll need a flow.ai account. Log into the dashboard and copy and paste the clientId form your Web API channel.

Start the connection

Import the LiveClient inside your AppDelegate file. The SDK opens a live connection so you should respect App events like applicationWillTerminate(_ application: UIApplication) to close the connection.

Simply call start() to open a connection and stop() to close it.

Example

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()
    }
}

Receiving events

The LiveClient supports a delegate you can implement with your view controller.

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])
}

Example

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]) {
    }
}

Sending messages

Use the send() method to send messages after you started the connection.

Example

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)

Loading history

You can load previous messages using loadHistory(). You can optionally provide a custom threadId to load messages of a specific thread or user.

SDK Reference

Read the complete SDK reference

Author

flow.ai

License

FlowCore is available under the MIT license. See the LICENSE file for more info.