npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cordova-watchconnectivity-plugin

v1.0.1

Published

cordova-watchconnectivity-plugin

Downloads

9

Readme

Cordova Watch Connectivity Plugin

Simple plugin that establishes iOS Watch Connectivity session with Watch OS and helps exchange of messages between a hybrid application and its Apple Watch application and vice-versa.

Installation

This is a fork of Gui Keller's plugin available here: https://github.com/guikeller/cordova-watchconnectivity-plugin

With cordova-cli

If you are using cordova-cli, install with:

cordova plugin add https://github.com/JoshuaRileyDev/cordova-watchconnectivity-plugin.git

With ionic

With ionic:

ionic cordova plugin add https://github.com/JoshuaRileyDev/cordova-watchconnectivity-plugin.git

With NPM

With NPM:

npm i cordova-watchconnectivity-plugin

Use from Javascript

Edit www/js/index.js and add the following code inside onDeviceReady

    // Receiving messages from Watch :: AppleWatch -> iPhone
    var receiveMessageSuccess = function(message){
        // Either from a sendMessage (with or without handlers) or updateApplicationContext
        var value = JSON.stringify(message);
        alert("Received message from Apple Watch : "+value);
    };
    var receiveMessageFailure = function(){
        alert("Could not receive message from Apple Watch");
    };

    // Sending Messages to Watch :: iPhone -> AppleWatch
    var sendMessageSuccess = function() {
        alert("Message sent successfully!");
    };
    var sendMessageFailure = function(){
        alert("Could not send message to Apple Watch.");
    };
    
    // Initialised WatchConnectivity Session successfully
    var initWatchSuccess = function() {
        // Waits for 1sec, it 'should be enough' to establish a session with the watch
        window.setTimeout( () => {
	    // Sends a message through 'sendMessage' - it 'should be' connected now
            var message = {message: "hello from phone", value: "1234", foo: "bar"};
            WatchConnectivity.sendMessage(message, sendMessageSuccess, sendMessageFailure);
	    // Sends a message through 'updateApplicationContext'
	    WatchConnectivity.updateApplicationContext(message, sendMessageSuccess, sendMessageFailure);
        
	    // Register to receive messages
            WatchConnectivity.messageReceiver(receiveMessageSuccess, receiveMessageFailure);
	}, 1000);
    };
    var initWatchFailure = function() {
        alert("Could not connect to Apple Watch.");
    };
    
    // Starts things up - WCSession.default
    WatchConnectivity.init(initWatchSuccess, initWatchFailure);

Use from Apple Watch extension

Swift

// Setup and activate session in 'awakeWithContext' or 'willActivate' methods
if (WCSession.isSupported()) {
    let session = WCSession.defaultSession();
    session.delegate = self;
    session.activateSession();
}

// Implement activationDidCompleteWith to know when it has been paired
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
    // Session started :: phone x watch
    if (error != nil) {
        print("InterfaceController :: session :: error: ", error);
    } else {
        print("InterfaceController :: session :: state: ", activationState);
    }
}


// - - - RECEIVING MESSAGES 'FROM' CORDOVA (JS)

// Implement didReceiveMessage WatchConnectivity - handler/callback to receive its incoming messages
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) {
    // Receiving messages from iPhone
    print("InterfaceController :: session :: didReceiveMessage: ", message);
}

// Implement didReceiveApplicationContext WatchConnectivity - handler/callback to receive its incoming messages
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
    // Receiving messages from iPhone
    print("InterfaceController :: session :: updateApplicationContext: ", message);
}


// - - - SENDING MESSAGES 'TO' CORDOVA (JS)

// Sending a message to iPhone - WCSession.default.sendMessage
func sendMessageNowWithHandlers() {
    let message = ["message": "hello from watch", "value": "4321", "bar": "foo"];
    WCSession.default.sendMessage( 
        message,
        replyHandler: { (response) -> Void in
            print("InterfaceController :: sendMessageNow :: Send message success : \(response)")
        },
        errorHandler: { (error) -> Void in
            print("InterfaceController :: sendMessageNow :: An error happened: \(error)")
        }
    );
}

// Sending a message to iPhone - WCSession.default.sendMessage
func sendMessageNowWithoutHandlers() {
    let message2 = ["message2": "hello from watch", "value2": "4321", "bar2": "foo"];
    WCSession.default.sendMessage(message2, replyHandler:nil);
}

// Sending a message to iPhone - WCSession.default.updateApplicationContext
func sendMessage() {
    do {
        let message3 = ["message3": "hello from watch", "value3": "54321", "bar3": "foo"];
	try WCSession.default.updateApplicationContext(message3);
    } catch {
        print("InterfaceController :: sendMessage :: error: ", error.localizedDescription);
    }
}

EXTRA INFO - See the following link: https://developer.apple.com/documentation/watchconnectivity/wcsession

Objective-C


// FIXME - Help wanted! Translate the code above from Swift to Objective-C
// Send through a PR and I will happily merge it, thank you very much.

Credits

Gui Keller

More Info

TODO: The plugin is very simple and short without much error handling.

Project supported by JetBrains