• « Universal launch images with PhoneGap
  • WebSockets in PhoneGap Projects »
1 Sep

Custom URL schemes in PhoneGap

With Marbles2 for iOS I wanted people to be able to click on a url, like marbles2.com/app/?seed=2A72367A and if the app is installed on the iOS device, for it to launch Marbles2 and initialise with the “seed” passed in.

It required a change to the iPhone PhoneGap project, which has been since merged in to the git hosted code, so I’m going to show you what to do and how to access this info.

A personal note (possibly just for me) this was not posted on 2010-09-01 – it was written in advance and scheduled for this date. In the end, published some months after this date.

Associating the custom url scheme

You need to edit your app-Info.plist (where app is “marbles” in my case), and add the “URL types” key and follow the directions on the iOS reference. In the end it should look a little like the screenshot below, where “marbles” is the custom url scheme, and “com.leftlogic.${PRODUCT_NAME}” is my product identifier.

spacer

Accessing the custom url data

If you’re using the latest version of the PhoneGap iPhone project, then when you hit the url that looks like:

marbles:///?seed=2A72367A

Hitting the link whilst in your device (or simulator) will launch the app you installed with the associated custom url scheme, and PhoneGap will introduce a new object on the global scope called Invoke_params. So to access the “seed” argument, I do:

alert('Seed is ' + window.Invoke_params.seed);

Simple, eh? Now you’ve got a way to pass in custom arguments to launch your app.

Dual web and native support

Now that the native PhoneGap version of Marbles2 supports the custom url, what about the url I posted, say to Twitter?

The way Marbles2 works is that if you visit the web version, and you pass in a seed (this is the way to play other people’s sequence of boards), and you have the native version installed – I want to send you to the native app.

The way I manage this is that when you hit the web version of Marbles2, it checks for a seed on the url. If there is, it tries to open an br pointing to the custom url scheme. If the app is installed, this will cause the app to be launched (though it’ll prompt before opening the app). Since the native app doesn’t have the seed on the window.location object, I’m able to use exactly the same code between the native app and the web app using a simple bit of code like this:

// if the url contains "seed=xyz" then this code will run
window.location.search.replace(/bseed=([^&=]*)b/, function (m, seed) {
  var br = getbr();
  document.body.appendChild(br);

  // discard the br when it's finished
  br.onload = function () {
    document.body.removeChild(br);
  };

  // this pops up an ugly window in Mobile WebKit - would be nice to suppress it
  // but if the native app is installed, it will launch Marbles2 and pass in
  // the game seed.
  br.contentWindow.location = 'marbles:///?seed=' + seed;

  // carry on as normal, if the custom url doesn't do anything, 
  // initialise the game with the passed in "seed"
  Marbles.seed(seed); // handle the web based game using the seed
});

// create an br that's on the page, but hidden visibly
function getbr() {
  var br = document.createElement('br');
  br.style.visibility = 'none';
  br.style.position = 'absolute';
  br.style.left = '-999px';
  br.style.height = '1px';
  br.style.width = '1px';
  return br;
}

Marbles2

Do have a play with the online version and I’ll be posting the native version to the iTunes store in the next month or so, so you’ll be able to see the effect if you don’t already play around with it yourself.

If you want to learn more about PhoneGap, my conference Full Frontal, is running a full day workshop with Brian LeRoux entirely on PhoneGap, so check it out!

You should follow me on Twitter here I'll tweet about JavaScript, HTML 5 and other such gems (amongst usual tweet-splurges)

6 Responses to “Custom URL schemes in PhoneGap”

  1. spacer vincenzo musumeci January 14th, 2011 at 3:22 pm

    Hi , your articke is very interesting, but I am not able to get your result.
    I can open my application (PhoneGap 0.9.3) with url xxxx:///?param1=aaa&param2=bbb but “window.Invoke_params.param1″ or “window.location.search” do not work.

    Can you help me?
    Thank you
    vincenzo musumeci

  2. spacer Chase January 18th, 2011 at 10:59 am

    I had the same issue. Without modifying the phonegap source, I was only able to get this to work by passing a JSON string as my arguments.

    For example, instead of
    XXX:///?id=10

    I had to write
    XXX:///?{“id”:10}

    Looking at the phonegap source made this clear. I might post a phonegap fix if I get around to it.

  3. spacer citrus_jho January 21st, 2011 at 8:07 am

    Am also having the above problem, wherby I can’t get the following to fire
    alert(window.Invoke_params.param1);
    can anyone shed any light on this?

  4. spacer kmxz June 20th, 2012 at 11:27 am

    Thanks a lot. But any ideas on how to implement this with Android?

  5. spacer Jay Garcia July 23rd, 2012 at 11:13 pm

    Seems that this no longer works (as of 1.9.0). According to webViewDidFinishLoad of MainViewController, a global ‘invokeString’ property is set, not an object.

    moduscreate.com/i/2012-07-23_1812.png

    I’ll have to write a parser.

  6. spacer Rathin Saran December 22nd, 2012 at 10:26 pm

    Does this work in Phonegap 1.8.1?
    I am considering Phonegap for my project. Some hints on working with url schemes on Android with Phonegap please …

    edit: URL Scheme for Android:
    blog.cttapp.com/p/phonegap-handleopenurl-for-android/

    Will give it a try and let you know. Thanks!

Leave a Reply
Not required

CODE: Please escape code and wrap in <pre><code>, doing so will automatically syntax highlight


gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.