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 ... :)
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:
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')
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With