如何将parameter passing给基于Phonegap的应用程序

我正在编写一个应用程序,使用JQM和Phonegap部署在iOS上,我需要它来读取input参数,如一个常见网站的url参数在JavaScript中处理对象'window.location.search'

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

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

这是现在正在工作,我已经可以打电话给我的应用程序,我现在需要的是阅读arg1,arg2等参数。我试过阅读window.location.search但没有运气。

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

任何build议,将不胜感激。

谢谢。

我的问题解决了使用这个链接的内容: 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]; } 

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

 function onDeviceReady() { alert(invokeString); } 

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

只是用来调用它的相同的string… 🙂

我有同样的问题,在这些答案的一切都是海拉迷惑和额外的信息。

通过两个简单的步骤来理解和解决问题:

  1. 信息丰富(如果你不在意背后发生了什么,你可以跳过):在项目中的Clases文件夹中findAppDelegate.m并search“handleOpenUrl”,你应该注意到有一些代码在说明怎么了注释。 我不知道Objective-C,但直观地说,那里的代码寻找window.handleOpenURL函数,并调用它给它的URL参数调用(例如'myapp:///?parameter = value')

  2. 基本上所有你需要做的是在全局(在窗口对象中)定义函数handleOpenURL

     function handleOpenURL (url) { alert(url); } 

请注意,这只会在您的应用程序打开时被执行

 <a href="myapp://">..</a> 

window.location将是您的phonegap index.html文件的位置,而不是用于启动您的应用程序的URL。

一些networkingsearchbuild议,一个函数称为:

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

可能会自动被调用。 我没有我的开发机器在这里testing虽然,对不起!

如果这在Objective-C中不起作用,请检查AppDelegate.m的handleOpenUrl方法。当您的应用程序以URLscheme打开时,这将被调用。

你应该在obj-c中完成,然后用一个插件将它传递给javascript代码:首先你需要实现obj-c

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

然后你可以像这样访问参数:

 - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { if ([[url scheme] isEqualToString:@"myapp"]) { //in here you do whatever you need the app to do // eg decode JSON string from base64 to plain text & parse JSON string } return YES; //if everything went well } 
 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"); });