【问题标题】:Send parameters to a local file in a UIWebView将参数发送到 UIWebView 中的本地文件
【发布时间】:2010-11-16 14:47:39
【问题描述】:

我正在开发一个原生 iPhone 应用程序。我能够将 local 文件 index.html 加载到 UIWebView 中:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];

网络加载正常。现在,我想用一些参数加载网络,就像在浏览器中键入这个一样: URL?var1=myValue&var2=myValue2

所以我可以在 html 文件的 javascript 中获取这些值。有没有办法将参数发送到本地html文件?

谢谢!

【问题讨论】:

    标签: iphone objective-c ios uiwebview


    【解决方案1】:

    仅在 objC 中最好且真正有效的答案(请记住保留您的文件夹结构 - 使用“为任何添加的文件夹创建文件夹引用”将文件复制到 xcode)

    NSString *path = [[NSBundle mainBundle]
           pathForResource:@"YOUR-HTML-FILE"
           ofType:@"html"
           inDirectory:@"YOUR-FOLDER" ]; 
     NSURL *url = [NSURL fileURLWithPath:path];
    
    
    
     NSString *theAbsoluteURLString = [url absoluteString];   
    
     NSString *queryString = @"?var=1232123232"; 
    
     NSString *absoluteURLwithQueryString = [theAbsoluteURLString stringByAppendingString: queryString];  
    
     NSURL *finalURL = [NSURL URLWithString: absoluteURLwithQueryString];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:finalURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:(NSTimeInterval)10.0 ];
    
    [web loadRequest:request];
    

    【讨论】:

    • 这个分析器帮了我很多
    【解决方案2】:

    Marjan 的方式是让它发挥作用的正确方式。

    正如我的朋友 Terence 指出的那样,诀窍是使用绝对文件 uri (file://) 字符串 从字符串正确创建 url 时。这边走 ?字符未编码,webview 可以正常加载文件。

    [NSURL URLWithString: @"file://localhost/Users/sj/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/../PrintingWebView.app/final.html?slide=2"];
    

    或者使用 URLWithString:relativeToURL 方法

    NSURL *relativeURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    NSURL *u = [NSURL URLWithString:@"html/index.html?site=2" relativeToURL:relativeURL];
    

    【讨论】:

    • 更简单的解决方案!
    【解决方案3】:

    您正在从自己的包中加载 HTML,这意味着您可以完全控制它。为什么不用你想要的字段参数化 HTML。您可以轻松添加一些隐藏的输入字段,然后您的 javascript 可以访问这些字段。假设您的 HTML 如下所示:

    <body>
    <form>
    Static Field 1: <input type="text" name="field1"><br>
    Static Field 2: <input type="text" name="field2"><br>
    %@
    </form>
    </body>
    

    现在只需将这些数据读入一个字符串,然后用一些隐藏字段格式化 %@:

    NSString *input = @"<input type=\"hidden\" name=\"field3\"><br>";
    NSString *formatted = [NSString stringWithFormat:html, input];
    

    然后在 webview 中调用 -loadHTMLString:。

    [webView loadHTMLString:formatted baseURL:nil];
    

    你有什么理由不能那样做。现在你的 javascript 可以通过调用来获取隐藏输入字段的值:

    document.getElementsByName('field3')[0].value;
    

    【讨论】:

      【解决方案4】:

      您可以在本地 html 文件中插入 javascript 来解析 GET 请求

      <script language="javascript">
      function getget(name) {
              var q = document.location.search;
              var i = q.indexOf(name + '=');
      
              if (i == -1) {
                  return false;
              }
      
              var r = q.substr(i + name.length + 1, q.length - i - name.length - 1);
      
              i = r.indexOf('&');
      
              if (i != -1) {
                  r = r.substr(0, i);
              }
      
              return r.replace(/\+/g, ' ');
          }
      </script>
      

      使用 getget('parameter_name') 从请求字符串中获取参数。

      【讨论】:

        【解决方案5】:

        不,很遗憾没有。

        但是,您可以通过使用 UIWebView:stringByEvaluatingJavaScriptFromString: 在 JavaScript 中设置变量和调用函数/方法来解决此问题

        【讨论】:

        • 我在本地 html 文件中定义了一个 javascript 函数。我可以加载那个文件,但是,我怎么能在那个文件中调用一个 javascript 函数呢?我需要一个例子。
        • [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"myJavascriptFunction(%f);", 1.0f]];
        • 你让它工作了吗?是否调用了 myJavascriptFunction 函数?在我的代码中它没有......我如何确定调用它是否正在完成?
        【解决方案6】:

        然后在 webview 中调用 -loadHTMLString:。

        我相信他避免使用 loadHTMLString 而不是 loadRequest,因为它会破坏 UIWebView 的后退和前进按钮

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-06
          • 2011-10-27
          • 2012-06-05
          • 2013-10-25
          相关资源
          最近更新 更多