【问题标题】:How to pass arguments to app built on Phonegap如何将参数传递给基于 Phonegap 构建的应用程序
【发布时间】:2012-09-14 21:09:26
【问题描述】:

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

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

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

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

我该怎么做?我需要编写一些 Objective C 代码吗?

任何建议将不胜感激。

谢谢。

【问题讨论】:

    标签: objective-c ios cordova jquery-mobile


    【解决方案1】:

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

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

    function onDeviceReady()
        {
            alert(invokeString);
        }
    

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

    只是用于调用它的相同字符串... :)

    【讨论】:

    • 其实它比我想象的要简单。我的 AppDelegate.m 文件已经为此准备好了,我只需要在 javascript 代码中使用 invokeString 即可,显然,这个变量只有在使用 url 调用应用程序时才会定义,因此需要检查它是否已定义: if (typeof invokeString != 'undefined') { //获取参数 }
    【解决方案2】:

    我遇到了同样的问题,这些答案中的所有内容都令人困惑和额外的信息。

    通过 2 个简单的步骤了解和解决问题:

    1. 信息丰富(如果你不关心后台发生的事情,你可以跳过): 转到项目中 Classes 文件夹中的 AppDelegate.m 并搜索“handleOpenUrl”,你应该注意到那里有一些带有 cmets 的代码来解释发生了什么。我不知道objective-c,但直观地说,那里的代码会查找window.handleOpenURL 函数并将其称为url 的参数(例如'myapp:///?parameter=value')

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

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

    请注意,这仅在您的应用使用

    打开时才会执行
    <a href="myapp://">..</a>
    

    【讨论】:

      【解决方案3】:

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

      一些网络搜索表明有一个函数叫做:

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

      .. 可能会自动被调用。不过我这里没有我的开发机器可以测试,抱歉!

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

      【讨论】:

      • 谢谢你,本。我会检查这些替代方案并在此处发布结果。
      • Ben,我尝试使用 then handleOpenURL 但也没有运气......但是,当我搜索它时,我发现了这个:gist.github.com/859540 .. 所以我试图用定义 didFinishLaunchingWithOptions 和 webViewDidFinishLoad 目标 c 方法的参数,这次变量 invokeString 是用我需要的确切 url 定义的:“myapp://?arg1=1&arg2=2”。
      【解决方案4】:

      您应该在 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
      }
      

      【讨论】:

      • 谢谢,豹猫。我做了非常类似的事情。我发现了这个:gist.github.com/859540 在尝试在 javascript(phonegap) 中调用 handleOpenURL 时,我仍然做不到,但我得到了 invokeString 变量,就像链接上的示例一样工作,并且完成了工作.. :)
      【解决方案5】:
      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");
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 2012-12-24
        • 2023-03-08
        • 2020-04-29
        • 2021-01-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多