如何将参数传递给构建在Phonegap上的应用程序

Vin*_*elo 7 objective-c ios jquery-mobile cordova

我正在写使用JQM和PhoneGap的应用程序在iOS部署,我需要它来读取输入参数,像一个普通网站的URL参数的处理对象window.location.search"在JavaScript并

在我的情况下,该应用程序将从一个网站启动,如下所示:

<a href="myapp://?arg1=1&arg2=2"> My App </a>
Run Code Online (Sandbox Code Playgroud)

这是正常的,我已经可以调用我的应用程序,我现在需要的是读取参数arg1,arg2等.我试过读window.location.search但没有运气.

我怎样才能做到这一点?我是否需要编写一些Objective C代码?

任何建议,将不胜感激.

谢谢.

Vin*_*elo 6

使用此链接的内容解决了我的问题:https://gist.github.com/859540

代码是:

Objective-c部分:

在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];
}
Run Code Online (Sandbox Code Playgroud)

在使用cordova-1.7.0的index.html文件中:

function onDeviceReady()
    {
        alert(invokeString);
    }
Run Code Online (Sandbox Code Playgroud)

alert返回:myapp://?arg1 = 1&arg2 = 2

只是用于调用它的相同字符串... :)