Latest 0.2.2
Homepage https://github.com/Narsail/relaykit
License MIT
Platforms ios 11.0, watchos 4.0
Authors

Alt text

Currently the WatchConnectivity Framework of iOS and watchOS can only handle one delegate. In the Application of my current Startup Evomo we have multiple frameworks independently talking to their counterpart on the Apple Watch. To handle the Messages between the Sender and its paired Receiver efficiently we developed RelayKit.

badge-pod badge-languages badge-pms ![badge-platforms] Build Status

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. To install RelayKit with CocoaPods:

  1. Make sure CocoaPods is installed.

  2. Update your Podfile to include the following:

    use_frameworks!
    pod 'RelayKit'
  3. Run pod install.

  4. In your code import RelayKit like so:
    import RelayKit

Usage

To supplement Watch Connectivity

Create a Relay

// On iOS Side
let core = WatchConnectivityCoreIOS()
let relay = Relay(core: core)

// You can access thie relay now anywhere through the shared variable
Relay.shared 

// WatchOS Side
let core = WatchConnectivityCoreWatchOS()
let relay = Relay(core: core)

Create and Register a Sender

// To actually send something let your class conform to the Sender protocol
class SenderExample: Sender {
    // This Ident is necessary to match it to its receiver pair
    var moduleIdent: String = "SamplePair"

    // This one is necessary for the internal mechanism, it can be nil, no default necessary
    var sendMessageBlock: ((Message, SendingMethod, @escaping (Message) -> Void, @escaping (Error) -> Void) -> Void)?

}

let sender = SenderExample()

// And register it to the relay
relay.register(sender)

Create and Register a Receiver


// The Receiver must conform to the Receiver Protocol

class SampleReceiver: Receiver {

    var moduleIdent: String = "SamplePair"

    var message: Message? = nil

    func didReceiveMessage(_ message: Message, _ method: SendingMethod, _ replyHandler: ((Message) -> Void)?) {
        self.message = message
    }

}

receiver = SampleReceiver()

relay.register(receiver)

Send a Message

// To send something it is important to use the correct SendingMethod of the Core you use
// for the WatchConnectivity Core it is as follows:

enum WatchConnectivityCoreMethod: SendingMethod {
        case sendMessage
        case transferUserInfo
}

// Conform to the Message Protocol
struct SampleMessage: Message {

    static var messageIdent: String = "SampleMessage"

    public let description: String

    func encode() -> [String : Any] {
        return [
            "description": description
        ]
    }

    static func decode(_ data: [String : Any]) throws -> SampleMessage {

        guard let description = data["description"] as? String else { throw MessageError.keyNotFound(key: "description") }

        return SampleMessage(description: description)

    }

}

// Send it
sender.sendMessage(SampleMessage(), WatchConnectivityCoreMethod.sendMessage)

Deregister


// If you are done you can deregister the sender and receiver
relay.deregister(sender)
relay.deregister(receiver)

Latest podspec

{
    "name": "RelayKit",
    "version": "0.2.2",
    "summary": "RelayKit helps you to send Messages from a Sender to a Receiver.",
    "description": "This Kit is especially designed for the Watch Connectivity Framework. While using the WC Framework in multiplenFrameworks of my own project i found the limitation of having only one delegate as a receiver. Therefore the neednto relay messages was born.",
    "homepage": "https://github.com/Narsail/relaykit",
    "license": {
        "type": "MIT"
    },
    "authors": {
        "David Moeller": "[email protected]"
    },
    "platforms": {
        "ios": "11.0",
        "watchos": "4.0"
    },
    "source": {
        "git": "https://github.com/Narsail/relaykit.git",
        "tag": "0.2.2"
    },
    "source_files": "RelayKit/Shared/**/*.{h,m,swift}",
    "ios": {
        "source_files": "RelayKit/iOS/**/*.{h,m,swift}"
    },
    "watchos": {
        "source_files": "RelayKit/watchOS/**/*.{h,m,swift}"
    },
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This