7

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.

Thanks.

User97693321
  • 3,336
  • 7
  • 45
  • 69
Vinicius Melo
  • 161
  • 1
  • 7

5 Answers5

6

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

The code is:

Objective-c part:

In MainViewController.m:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // 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 
{
     // only valid if ___PROJECTNAME__-Info.plist specifies a protocol to handle
     if (self.invokeString)
     {
        // this is passed before the deviceready event is fired, so you can access it in js when you receive deviceready
        NSString* jsString = [NSString stringWithFormat:@"var invokeString = \"%@\";", 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(invokeString);
    }

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

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

Vinicius Melo
  • 161
  • 1
  • 7
  • 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 } – Vinicius Melo Sep 16 '12 at 01:41
2

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) {
        alert(url);
    }
    

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

<a href="myapp://">..</a>
Andrei Cristian Prodan
  • 1,114
  • 4
  • 17
  • 34
0

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.

Ben Clayton
  • 80,996
  • 26
  • 120
  • 129
  • Thank You, Ben. I'll check out those alternatives and post here the results. – Vinicius Melo Sep 15 '12 at 02:17
  • Ben, I tried to use then handleOpenURL but with no luck too ... However, when I was googling about it, i found this: https://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". – Vinicius Melo Sep 16 '12 at 01:21
0

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

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    NSURL *urlToParse = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
        if (urlToParse) {
            [self application:application handleOpenURL:urlToParse];
        } 
        return YES;
}

and then you could access parameters like this :

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    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
}
Duke
  • 1,731
  • 2
  • 17
  • 33
  • Thanks, Ocelot. I did something very much like this. I found this: https://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.. :) – Vinicius Melo Sep 16 '12 at 01:15
0
function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
     return "";
  else
     return results[1];
  }
Call this function like var para1 = getParameterByName("para1"); 
on pageshow event in jquery mobile.
$('#page').on('pageshow',function(event){
   var para1 = getParameterByName("para1");
});
Ved
  • 2,701
  • 2
  • 22
  • 30