javascriptioscordovawatchkithandoff

How to Pass an NSURL from Watchkit - > Native Cordova app & open browser with Javascript


I have a working cordova app and have built a Watchkit app. Aware that I can't call the native app to foreground or do much of anything with its inner workings, I thought to use Handoff to simply send a user, via Safari, to a webpage which basically canabalizes code from the app and processes a URL query string parameter. So I wrote a routine which basically does this:

   var URLwithQuery = "http://www.example.com?query="+QueryVar

   var urlPath: String = URLwithQuery
   var url: NSURL = NSURL(string: urlPath)!

   updateUserActivity("com.example.www.hellobrowser", 
           userInfo: ["name":"WhereAmI"], webpageURL: url)

Now, I can't test that as I don't have a watch and the simulator doesn't support Handoff. But I doubt this is going to work: I've manually inserted an NSUserActivityTypes array and "hellobrowser" activity in the native app's root info.plist, but this is seriously undocumented territory and while everything compiles and runs, I'll have no idea if it actually triggers a handoff until I can run it on an actual watch. Murphy's law says it will fail.

An alternative way to do this would be to somehow pass the NSURL back to Cordova and use

window.open('http://www.example.com?Query='+QueryVar, '_system');

For that to work I was to use this Cordova -Watchkit plugin which allows passing of JSON objects between the Watchkit extension and the javascript in the cordova app. So on the native app side I'd implement a version of this messaging example to pass the stringified NSURL (except this example is iPhone -> iPhone rather than Watchkit -> iPhone):

applewatch.init(function (appGroupId) {
    alert(appGroupId);

    applewatch.addListener("myqueue", function(message) {
        alert("Message received: " + message);
    });

    applewatch.sendMessage("test", "myqueue");
});

However, I'm a noob at passing JSON objects and there's no example of what the Swift code in the Watchkit app or extension might look like to pass an NSURL back to that listener.

I suspect there's an easier way to do this!! If I can just get that NSURL from the Watchkit app as a string and into the javascript of my native app, I'm golden from there.


Solution

  • In your WatchKit Extension WKInterfaceController, you will want to reference the MMWormhole lib (compiled and included in the plugin) and do something like:

    var wormhole: MMWormhole!
    
    wormhole = MMWormhole(applicationGroupIdentifier: "group.com.hellobrowser",
        optionalDirectory: nil)
    
    wormhole.listenForMessageWithIdentifier("myqueue", listener: yourHandler)
    

    Then to send a message from your Cordova app via the Apple Watch plugin so that it's picked up by the WatchKit Extension listener:

    applewatch.init(function () {
        applewatch.sendMessage("test", "myqueue");
    }, null, "group.com.hellobrowser");
    

    Alternatively, if you want to set values that are readable / writeable from both your Cordova app and WatchKit Extension, you may want to look at using NSUserDefaults, which were only implemented in the plugin since this question was posted.

    I hope this helps, a full sample app or detailed blog post will be available when I get around to it and when I know Apple with approve these methodologies.