Cordova Watch Connectivity Plugin
Simple plugin that establishes iOS Watch Connectivity session with Watch OS 2 and helps exchange of messages between an iPhone hybrid application and its iWatch application.
Installation
With cordova-cli
If you are using cordova-cli, install with:
cordova plugin add https://github.com/DVenkatesh/cordova-plugin-watchconnectivity.git
With plugman
With a plain plugman, you should be able to install with something like:
plugman --platform <ios> --project <directory> --plugin https://github.com/DVenkatesh/cordova-plugin-watchconnectivity.git
Use from Javascript
Edit www/js/index.js
and add the following code inside onDeviceReady
var { var obj = JSON; ; } var { ; } var { ; } var { sswc; sswc; } sswc;
Use from iWatch extension
Objective-C
// Setup and activate session in awakeWithContext or willActivate if () // Implement didReceiveMessage WatchConnectivity handler/callback to receive incoming messages - (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *, id> *)message replyHandler:(void(^)(NSDictionary<NSString *, id> *replyMessage))replyHandler // Send message - (void)sendMessage:(NSString*)message
Swift
// Setup and activate session in awakeWithContext or willActivateif WCSession.isSupported() { let session = WCSession.defaultSession() session.delegate = self session.activateSession()}// Implement didReceiveMessage WatchConnectivity handler/callback to receive incoming messagesfunc session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) { let message = message["message"] as? String print(message) self.sendMessage("Message from iWatch")}// Send messagefunc sendMessage:(message: String) -> Void{ let message = ["message": message] WCSession.defaultSession()!.sendMessage(["message": message], replyHandler: { (response) -> Void in print("Send message success") }, errorHandler: { (error) -> Void in print("Send message failed") }) }
Credits
Written by Venkatesh D and Vagish M M
More Info
TODO: The plugin is very simple and short without much error handling. This is developed for an immediate need and shall be upgraded to support other platforms with error handling and improved design.
For more information on setting up Cordova see the documentation
For more info on plugins see the Plugin Development Guide