How to pass arguments to app built on Phonegap

2020-06-16 05:39发布

问题:

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.

回答1:

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 ... :)



回答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>


回答3:

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.



回答4:

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
}


回答5:

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");
});