Latest 1.1.0
Homepage https://github.com/nickromano/WatchSync
License MIT
Platforms ios 9.3, watchos 3.0
Dependencies GzipSwift
Frameworks WatchConnectivity
Authors

⌚️WatchSync

CocoaPods
Carthage Compatible

WatchConnectivity wrapper with typed messages, better error handling, and simplified subscription APIs.

Example

Send messages

Create a new message type that conforms to the SyncableMessage protocol. Uses Codable under the hood.

import WatchSync

struct MyMessage: SyncableMessage {
    var myString: String?
    var myDate: Date?
}

Send the message from anywhere in the iOS or watchOS app.

let myMessage = MyMessage(myString: "Test", myDate: Date())

WatchSync.shared.sendMessage(myMessage) { result in
}

You can also send a simple dictionary as well.

WatchSync.shared.sendMessage(["test": "message"]) { result in
}

Subscribe to new messages

Listen for changes from the paired device (iOS or watchOS)

class ViewController: UIViewController {
    var subscriptionToken: SubscriptionToken?

    override func viewDidLoad() {
        super.viewDidLoad()

        subscriptionToken = WatchSync.shared.subscribeToMessages(ofType: MyMessage.self) { myMessage in
            print(String(describing: myMessage.myString), String(describing: myMessage.myDate))
        }
    }
}

Update application context

WatchSync.shared.update(applicationContext: ["test": "context"]) { result in
}

Subscribe to application context updates

appDelegateObserver = 

class ViewController: UIViewController {
    var subscriptionToken: SubscriptionToken?

    override func viewDidLoad() {
        super.viewDidLoad()

        subscriptionToken = WatchSync.shared.subscribeToApplicationContext { applicationContext in
            print(applicationContext)
        }
   }
}

How it works

  • If the paired device is reachable, WatchSync will try to send using an interactive message with session.sendMessage().
  • If the paired device is unreachable, it will fall back to using sendUserInfo() instead.
  • All messages conforming to SyncableMessage will be JSON serialized to reduce the size of the payload. This is to reduce the likelyhood of running into a WCErrorCodePayloadTooLarge error.
  • For interactive messages it uses the replyHandler for delivery acknowledgments.

Installation & Setup

In your AppDelegate (iOS) and ExtensionDelegate (watchOS) under applicationDidFinishLaunching you will need to activate the Watch Connectivity session.

WatchSync.shared.activateSession { error in
    if let error = error {
        print("Error activating session (error.localizedDescription)")
        return
    }
    print("Activated")
}

Error handling

The sendMessage method returns a closure with a result to switch on that reduces the number of possible states and errors your app can end up in.

WatchSync.shared.sendMessage(myMessage) { result in
    switch result {
    case .failure(let failure):
        switch failure {
        case .sessionNotActivated:
            break
        case .watchConnectivityNotAvailable:
            break
        case .unableToSerializeMessageAsJSON(let error):
            break
        case .watchAppNotPaired:
            break
        case .watchAppNotInstalled:
            break
        case .unhandledError(let error):
            break
        case .badPayloadError(let error):
            break
        case failedToDeliver(let error):
            break
        }
    case .sent:
        break
    case .delivered:
        break
    }
}

Latest podspec

{
    "name": "WatchSync",
    "version": "1.1.0",
    "summary": "WatchConnectivity wrapper with typed messages, better error handling, and simplified subscription APIs.",
    "description": "Use WatchSync as the WatchConnectivity delegate for your application.  It allows you to send typed messages (using `Codable`), nreceive messages using closures anywhere in your application, and provides better error handling when connectivity isn't working.",
    "homepage": "https://github.com/nickromano/WatchSync",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Nick Romano": "[email protected]"
    },
    "platforms": {
        "ios": "9.3",
        "watchos": "3.0"
    },
    "source": {
        "git": "https://github.com/nickromano/WatchSync.git",
        "tag": "1.1.0"
    },
    "source_files": "Sources/**/*.{h,swift}",
    "swift_version": "4.1",
    "frameworks": "WatchConnectivity",
    "dependencies": {
        "GzipSwift": []
    }
}

Pin It on Pinterest

Share This