WatchConnectivity wrapper with typed messages, better error handling, and simplified subscription APIs.
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
}
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))
}
}
}
WatchSync.shared.update(applicationContext: ["test": "context"]) { result in
}
appDelegateObserver =
class ViewController: UIViewController {
var subscriptionToken: SubscriptionToken?
override func viewDidLoad() {
super.viewDidLoad()
subscriptionToken = WatchSync.shared.subscribeToApplicationContext { applicationContext in
print(applicationContext)
}
}
}
- If the paired device is reachable,
WatchSync
will try to send using an interactive message withsession.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 aWCErrorCodePayloadTooLarge
error. - For interactive messages it uses the
replyHandler
for delivery acknowledgments.
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")
}
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
}
}