Take the 2-minute tour ×

I'm writing an app using JQM and Phonegap to deploy on iOS and I need it to read input arguments like a url arguments of a common website does in javascript by handling the object 'window.location.search'

In my situation, the app will be launched from a website, like this:

<a href="myapp://?arg1=1&arg2=2"> My App </a>

This is working right now, I can already call my app, what I need now is to read the arguments arg1, arg2, etc. I've tried reading window.location.search but with no luck.

How can I do this? Do I need to write some Objective C code?

Any suggestions would be appreciated.


share|improve this question
add comment

5 Answers

up vote 2 down vote accepted

My problem was solved using the content of this link: https://gist.github.com/859540

The code is:

Objective-c part:

In MainViewController.m:

    // perform any custom startup stuff you need to ...
        // process your launch options
    NSArray *keyArray = [launchOptions allKeys];
    if ([launchOptions objectForKey:[keyArray objectAtIndex:0]]!=nil) 
               // we store the string, so we can use it later, after the webView loads
        NSURL *url = [launchOptions objectForKey:[keyArray objectAtIndex:0]];
        self.invokeString = [url absoluteString];
        NSLog(@amp;" launchOptions = %@",url); // if you want to see what is happening
    // call super, because it is super important ( 99% of phonegap functionality starts here )
    return [super application:application didFinishLaunchingWithOptions:launchOptions];

- (void) webViewDidFinishLoad:(UIWebView*) theWebView 
     if (self.invokeString)
        [theWebView stringByEvaluatingJavaScriptFromString:jsString];

     // Black base color for background matches the native apps
     theWebView.backgroundColor = [UIColor blackColor];

    return [super webViewDidFinishLoad:theWebView];

In the index.html file using cordova-1.7.0:

function onDeviceReady()

alert returned: myapp://?arg1=1&arg2=2

just the same string used to call it ... :)

share|improve this answer
Actually it was easiest than I thought. My AppDelegate.m file was already prepared for this, I just needed to use the invokeString in the javascript code, obviously, this variable will be defined only when the app is called with a url, so it is necessary to check if it is defined: if (typeof invokeString != 'undefined') { //get parameters } –  viniciusmelo Sep 16 '12 at 1:41
add comment

I had the same issue, everything here in these answers is hella confusing and extra information.

Understanding and solving the problem in 2 easy steps:

  1. Informative (you can skip if you don't care what happens in the background): Go to AppDelegate.m in Clases folder in the project and search for "handleOpenUrl", you should notice some code there with comments explaining what's up. I don't know objective-c, but intuitively that code there looks for window.handleOpenURL function and calls it giving it the parameter of the url called (e.g. 'myapp:///?parameter=value')

  2. Basically all you have to do is globally(in window object) define the function handleOpenURL

    function handleOpenURL (url) {

Note that this only gets executed when your app is opened with an

<a href="myapp://">..</a>
share|improve this answer
thank you! bunch of crazy people down there... –  Devin Smith Dec 1 '13 at 0:51
add comment

window.location will be the location of your phonegap index.html file, not the URL that was used to launch your app.

Some web searches suggested that a function called:

function handleOpenUrl(url) {
   alert("opened from url " + url);

.. might automatically be called . I don't have my dev machine here to test though, Sorry!

If this doesn't work in Objective-C check out the handleOpenUrl method of the AppDelegate.m This gets called when your app is opened with a URL Scheme.

share|improve this answer
Thank You, Ben. I'll check out those alternatives and post here the results. –  viniciusmelo Sep 15 '12 at 2:17
Ben, I tried to use then handleOpenURL but with no luck too ... However, when I was googling about it, i found this: gist.github.com/859540 .. so I tried to get the url with params defining the didFinishLaunchingWithOptions and webViewDidFinishLoad objective-c methods and this time the variable invokeString was defined with the exact url I needed : "myapp://?arg1=1&arg2=2". –  viniciusmelo Sep 16 '12 at 1:21
add comment

you should do it in obj-c and then with a plugin pass it to javascript code : for doing it in obj-c first you should implement

    NSURL *urlToParse = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
        if (urlToParse) {
            [self application:application handleOpenURL:urlToParse];
        return YES;

and then you could access parameters like this :

    if ([[url scheme] isEqualToString:@"myapp"]) {
        //in here you do whatever you need the app to do
        // e.g decode JSON string from base64 to plain text & parse JSON string
 return YES; //if everything went well
share|improve this answer
Thanks, Ocelot. I did something very much like this. I found this: gist.github.com/859540 while trying to do the handleOpenURL be called in javascript(phonegap), and, I still can't do it, but I get the invokeString variable working like the example on the link, and that did the job.. :) –  viniciusmelo Sep 16 '12 at 1:15
you are welcome –  Ocelot Sep 16 '12 at 6:54
add comment
function getParameterByName(name)
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
     return "";
     return results[1];
Call this function like var para1 = getParameterByName("para1"); 
on pageshow event in jquery mobile.
   var para1 = getParameterByName("para1");
share|improve this answer
add comment

Your Answer


