Skip to content
WatchConnectivity wrapper with typed messages, better error handling, and simplified subscription APIs
Branch: master
Clone or download
Pull request Compare This branch is 1 commit ahead, 5 commits behind nickromano:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Sources
WatchSync Example
WatchSync iOSTests
WatchSync.xcodeproj
WatchSync.xcworkspace
.gitignore
.swiftlint.yml
LICENSE
Podfile
Podfile.lock
README.md
WatchSync.podspec

README.md

⌚️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
    }
}
You can’t perform that action at this time.