Skip to content
This repository has been archived by the owner on Jul 23, 2020. It is now read-only.

theappbusiness/TABCommunicate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The App Business

Version License Platform

TABCommunicate

A lightweight, strongly typed Multipeer connectivity wrapper to allow sending an object between devices.

Usage

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

TABCommunicateConfiguration

Create a TABCommunicateConfiguration like so

let configuration = TABCommunicateConfiguration("myuniqueservice", numberOfRetryAttempts: 3, retryDelay: 1, password: "password")

The configuration object lets you describe how TABCommunicate will establish a connection to other devices and handle failures. numberOfRetryAttempts has a default value of 0 and retryDelay has a default value of 1.

TABCommunicator

To send and receive objects create and retain an instance of TABCommunicator passing a configuration. When we create an instance we define what object we want to send. This Object MUST conform to the TABCommunicable protocol. There are two ways to initialize an instance of TABCommunicator depending on how you want to receive objects. The first specifies a delegate object that conforms to the TABCommunicatorDelegate protocol,

let communicator = TABCommunicator<SomeTABCommunicable>(configuration, delegate: self)

The second passes a block (which is captured strongly) which handles the object being received.

let communicator = TABCommunicator<SomeTABCommunicable>(configuration) { someCommunicable in
  print(someCommunicable)
}

Send an object to connected peers with the following

communicator.sendCommunicableObject(myObject) { result in
  switch result {
  case .Success:
    //Do something
  case .Failure(let error):
    //Handle error
  }
}

TABCommunicable

In order to for a object to be sent and received it must conform to the TABCommunicable protocol which requires the two functions

public protocol TABCommunicable {
  static func create(data: NSData) -> Self
  func dataRepresentation() throws -> NSData
}

TABCommunicatorDelegate

The TABCommunicatorDelegate will receive updates when an object is sent and when the connection did update.

extension ViewController: TABCommunicateDelegate {
  func CommunicableObjectRecieved(object: SomeTABCommunicable) {
    //Do something
  }

  func connectionDidUpdate(connected: Bool) {
    //Do something
  }
}

Requirements

iOS 9 or later

Installation

TABCommunicate is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "TABCommunicate"

Author

Neil3079, neil.horton@theappbusiness.com

License

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

About

Lightweight strongly typed wrapper around Multipeer Connectivity

Resources

License

Stars

Watchers

Forks

Packages

No packages published