我正在使用 JQM 和 Phonegap 编写一个应用程序以部署在 iOS 上,我需要它通过处理对象“window.location.search”来读取输入参数,就像普通网站在 javascript 中所做的 url 参数一样
在我的情况下,应用将从网站启动,如下所示:
<a href="myapp://?arg1=1&arg2=2"> My App </a>
这现在正在工作,我已经可以调用我的应用程序,我现在需要的是读取参数 arg1、arg2 等。我尝试读取 window.location.search 但没有成功。
我该怎么做?我需要编写一些 Objective C 代码吗?
如有任何建议,我们将不胜感激。
谢谢。
最佳答案
使用此链接的内容解决了我的问题: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
只是用来调用它的相同字符串... :)
关于objective-c - 如何将参数传递给基于 Phonegap 构建的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12432004/