Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何将参数传递给基于Phonegap构建的应用程序_Objective C_Ios_Cordova_Jquery Mobile - Fatal编程技术网

Objective c 如何将参数传递给基于Phonegap构建的应用程序

Objective c 如何将参数传递给基于Phonegap构建的应用程序,objective-c,ios,cordova,jquery-mobile,Objective C,Ios,Cordova,Jquery Mobile,我正在编写一个应用程序,使用JQM和Phonegap在iOS上部署,我需要它通过处理对象“window.location.search”读取输入参数,就像javascript中常见网站的url参数一样 在我的情况下,该应用程序将从网站启动,如下所示: <a href="myapp://?arg1=1&arg2=2"> My App </a> 这是现在工作,我已经可以调用我的应用程序,我现在需要的是阅读参数arg1,arg2等。我已经尝试阅读window.lo

我正在编写一个应用程序,使用JQM和Phonegap在iOS上部署,我需要它通过处理对象“window.location.search”读取输入参数,就像javascript中常见网站的url参数一样

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

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

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

我该怎么做?我需要写一些客观的C代码吗

如有任何建议,将不胜感激


谢谢。

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

一些web搜索建议使用一个名为:

function handleOpenUrl(url) {
   alert("opened from url " + url);
}
。。可能会自动调用。我没有我的开发机器在这里测试,虽然,对不起


如果这在Objective-C中不起作用,请检查AppDelegate.m的handleAppEnUrl方法。当您的应用程序使用URL方案打开时,将调用该方法

您应该在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
        // e.g decode JSON string from base64 to plain text & parse JSON string
    }
 return YES; //if everything went well
}

使用此链接的内容解决了我的问题:

代码是:

目标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


与用于调用它的字符串相同…:)

我也有同样的问题,这些答案中的所有内容都是令人困惑的额外信息

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

  • 信息性(如果您不关心后台发生了什么,可以跳过):转到项目中Clases文件夹中的AppDelegate.m并搜索“handleOpenUrl”,您应该会注意到一些代码,其中有注释解释了发生了什么。我不知道objective-c,但从直觉上看,那里的代码查找window.handleOpenURL函数并调用它,给它一个url的参数(例如“myapp://?parameter=value”)

  • 基本上只需全局(在窗口对象中)定义函数handleOpenURL

    function handleOpenURL (url) {
        alert(url);
    }
    
  • 请注意,这仅在应用程序打开时执行

    <a href="myapp://">..</a>
    
    
    
    谢谢你,本。我会检查这些替代方案,并将结果发布在这里。本,我尝试使用then HandleOpeEnurl,但也没有运气。。。然而,当我在谷歌上搜索时,我发现:。。因此,我尝试使用定义didFinishLaunchingWithOptions和webViewDidFinishLoad objective-c方法的参数来获取url,这次变量invokeString是使用我需要的确切url来定义的:“myapp://?arg1=1&arg2=2”。谢谢,Ocelot。我做了非常像这样的事情。我发现:在尝试使用javascript(phonegap)调用handleOpenURL时,我仍然无法做到这一点,但我得到了invokeString变量,就像链接上的示例一样,这就完成了工作:)事实上,这比我想象的要容易。我的AppDelegate.m文件已经为此做好了准备,我只需要在javascript代码中使用invokeString,显然,只有在使用url调用应用程序时才会定义此变量,因此有必要检查它是否已定义:if(typeof invokeString!=“undefined”){//get parameters}
    <a href="myapp://">..</a>