A lightweight, strongly typed Multipeer connectivity wrapper to allow sending an object between devices.
To run the example project, clone the repo, and run pod install
from the Example directory first.
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.
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
}
}
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
}
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
}
}
iOS 9 or later
TABCommunicate is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "TABCommunicate"
Neil3079, neil.horton@theappbusiness.com
TABCommunicate is available under the MIT license. See the LICENSE file for more info.