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.
If you are using cordova-cli, install with:
cordova plugin add https://github.com/DVenkatesh/cordova-plugin-watchconnectivity.git
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
Edit www/js/index.js
and add the following code inside onDeviceReady
var didReceiveMessage = function(message){
var obj = JSON.parse(message);
alert(obj.message);
}
var msgSendSuccess = function() {
alert("Message send success");
}
var failure = function() {
alert("Error");
}
var success = function() {
sswc.messageReceiver(didReceiveMessage, failure);
sswc.sendMessage("Message from phone", msgSendSuccess, failure);
}
sswc.init(success, failure);
// Setup and activate session in awakeWithContext or willActivate
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
// 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 {
NSString *message = [message objectForKey:@"message"];
NSLog(@"%@",message);
[self sendMessage:@"Message from iWatch"];
}
// Send message
- (void)sendMessage:(NSString*)message {
NSDictionary *messageDictionary = [[NSDictionary alloc] initWithObjects:@[message] forKeys:@[@"message"]];
[[WCSession defaultSession] sendMessage:messageDictionary
replyHandler:^(NSDictionary *reply) {
NSLog(@"Send message success");
}
errorHandler:^(NSError *error) {
NSLog(@"Send message failed");
}
];
}
// Setup and activate session in awakeWithContext or willActivate
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
// Implement didReceiveMessage WatchConnectivity handler/callback to receive incoming messages
func 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 message
func 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")
})
}
Written by Venkatesh D and Vagish M M
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