如何传递参数到PhoneGap上构建的应用程序 [英] How to pass arguments to app built on Phonegap
问题描述
我正在使用JQM和Phonegap编写一个应用程序来部署在iOS上,我需要它来读取输入参数,像一个常见网站的URL参数在javascript中通过处理对象'window.location.search'
在我的情况下,应用程序将从网站启动,如下所示:
< a href =myapp://?arg1 = 1& arg2 = 2>我的应用程式< / a>
现在工作正常了,我已经可以打电话给我的应用程序了,参数arg1,arg2等等我试过阅读window.location.search但没有运气。
我该如何做?我需要写一些Objective C代码吗?
任何建议都会感激。
p>
我的问题已解决使用此链接的内容: https://gist.github.com/859540
代码为:
Objective-c part:
在MainViewController.m中:
- (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//执行任何自定义启动的东西你需要...
//处理你的启动选项
NSArray * keyArray = [launchOptions allKeys];
if([launchOptions objectForKey:[keyArray objectAtIndex:0]]!= nil)
{
//我们存储字符串,所以我们可以使用它后,webView加载
NSURL * url = [launchOptions objectForKey:[keyArray objectAtIndex:0]];
self.invokeString = [url absoluteString];
NSLog(@amp;launchOptions =%@,url); //如果你想看看发生了什么
}
//调用super,因为它是非常重要的(99%的phonegap功能从这里开始)
return [super application:application didFinishLaunchingWithOptions: launchOptions];
}
- (void)webViewDidFinishLoad:(UIWebView *)theWebView
{
//仅当___ PROJECTNAME __- Info.plist指定协议处理
if(self.invokeString)
{
//这是在deviceready事件被触发之前传递的,所以你可以在js中访问它,当你收到deviceready
NSString * jsString = [NSString stringWithFormat:@var invokeString = \%@ \;,self.invokeString];
[theWebView stringByEvaluatingJavaScriptFromString:jsString];
}
//背景的黑色基色匹配原生应用程序
theWebView.backgroundColor = [UIColor blackColor];
return [super webViewDidFinishLoad:theWebView];
}
在使用cordova-1.7.0的index.html文件中:
function onDeviceReady()
{
alert(invokeString);
}
警报已返回:myapp://?arg1 = 1& arg2 = / p>
只是使用相同的字串来呼叫...:)
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.
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 ... :)
这篇关于如何传递参数到PhoneGap上构建的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!