Spreadbot client for iOS/OS X


Keywords
client-library, ios, polling, server-sent-events, spreadbot, sse, swift, websockets, wss
License
MIT
Install
pod try Spreadbot_iOS

Documentation

Spreadbot_iOS

Spreadbot client for iOS

Requirements

  • iOS 9.0+
  • Xcode 8.0+
  • Swift 3.0+

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

CocoaPods 1.1.0+ is required to build Spreadbot_iOS 1.0.0+.

$ pod setup
$ pod init

To integrate Spreadbot_iOS into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.1'
use_frameworks!

pod 'Spreadbot_iOS', '~> 1.1.1'

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Spreadbot_iOS into your Xcode project using Carthage, specify it in your Cartfile:

github "spreadbot/Spreadbot_iOS" ~> 1.1.1

Swift Package Manager

To use Spreadbot_iOS as a Swift Package Manager package just add the following in your Package.swift file.

import PackageDescription

let package = Package(
    name: "HelloSpreadbot_iOS",
    dependencies: [
        .Package(url: "https://github.com/Spreadbot/Spreadbot_iOS.git", "1.1.1")
    ]
)

Manually

If you prefer not to use either of the aforementioned dependency managers, you can integrate Spreadbot_iOS into your project manually.

Git Submodules

  • Open up Terminal, cd into your top-level project directory, and run the following command "if" your project is not initialized as a git repository:
$ git init
  • Add Spreadbot_iOS as a git submodule by running the following command:
$ git submodule add https://github.com/Spreadbot/Spreadbot_iOS.git
$ git submodule update --init --recursive
  • Open the new Spreadbot_iOS folder, and drag the Spreadbot_iOS.xcodeproj into the Project Navigator of your application's Xcode project.

    It should appear nested underneath your application's blue project icon. Whether it is above or below all the other Xcode groups does not matter.

  • Select the Spreadbot_iOS.xcodeproj in the Project Navigator and verify the deployment target matches that of your application target.

  • Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.

  • In the tab bar at the top of that window, open the "General" panel.

  • Click on the + button under the "Embedded Binaries" section.

  • You will see two different Spreadbot_iOS.xcodeproj folders each with two different versions of the Spreadbot_iOS.framework nested inside a Products folder.

    It does not matter which Products folder you choose from.

  • Select the Spreadbot_iOS.framework.

  • And that's it!

The Spreadbot_iOS.framework is automagically added as a target dependency, linked framework and embedded framework in a copy files build phase which is all you need to build on the simulator and a device.

Embeded Binaries

  • Download the latest release from https://github.com/Spreadbot/Spreadbot_iOS/releases
  • Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.
  • In the tab bar at the top of that window, open the "General" panel.
  • Click on the + button under the "Embedded Binaries" section.
  • Add the downloaded Spreadbot_iOS.framework.
  • And that's it!

Usage

Spreadbot API

// Import 
import Spreadbot_iOS

// Setup & Configuration
let spreadbot: Spreadbot = Spreadbot(
    atPath: "iosTest",
    baseURLString: "http://wwww.my-spreadbot-server.com", 
    authURLString: "http://wwww.my-spreadbot-auth-server.com", 
    accessToken: "myAccessToken", 
    refreshToken: "myRefreshToken", 
    base64Credentials: "MyBase64Credentials", 
    log: true // should be false in production
)

// Add Listeners                            
spreadbot.onOpen = {
  print("connection establised")
}
        
spreadbot.onError = { (error) in
  print("connection error received \(error)")
}

spreadbot.onMessage = { (id, data) in
  print("message received: \(id, data)")
}
                
// Connect
spreadbot.establishConnection()                

// Send String Message
spreadbot.send(message: "myMessageTest")
                
// Send Binary Message
let dataOne = Data(bytes: bytes, count: length)
spreadbot.send(message: dataOne)

// GET
spreadbot.getData(onSuccess: {_ in }, onError: {_ in })
                
// POST
let dataTwo = Data(bytes: bytes, count: length)
spreadbot.postData(withPayload: dataTwo, onSuccess: {_ in}, onError: {_ in })

// Disconnect
spreadbot.closeConnection()

Reconnection

Sever Side Events (SSE) are used as a fallback if a Websocket connection cannot be established after 3 attempts.

In turn, HTTP polling will be attempted if a SSE connection cannot be established, polling every 15 seconds for new data.

If a Websocket connection cannot be established, a POST request will be attempted to send the data. If the app is offline, requests will be retried when the network becomes reachable again.

If the app goes offline the Spreadbot library will attempt to reconnect when it detects that the network is reachable again.

ToDo

- watchOS, macOS (OS X) and tvOS
- Linux support
- Add more tests
- SSL support
- Custom queues?

Credits

Spreadbot_iOS by Spreadbot.io makes use of the following libraries:

License

Spreadbot_iOS is released under the MIT license. See LICENSE for details.