Skip to content

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.

License

Notifications You must be signed in to change notification settings

Narsail/relaykit

Repository files navigation

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.

  1. 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)

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published