将变量从 JavaScript 传递到 Objective-C - JSON

Pass variable from JavaScript to Objective-C - JSON?

本文关键字:Objective-C JSON 变量 JavaScript      更新时间:2023-09-26

可能的重复项:
如何将值从javascript传递到objective-C

我想将文本变量从JavaScript传递给Objective-C变量。

我的 HTML 文件如下所示:

<p>Bitte tragen Sie die Document ID ein:</p>
<SCRIPT TYPE="text/javascript">
    function testResults(form){
        location.href = DocumentID.value;
    }
</SCRIPT>
<form action="">
  <table border="0" cellpadding="5" cellspacing="0" bgcolor="#E0E0E0">
    <tr>
      <td align="right">Document ID:</td>
      <td><input name="DocumentID" type="text" size="30" maxlength="30" value="35060e6242160705744ffd8e280005b9"></td>
    </tr>
        <tr>
      <td align="right">Formular:</td>
      <td>
        <input type="button" NAME ="button" value=" Absenden " onClick="testResults(this.form)">
        <input type="reset" value=" Abbrechen">
      </td>
    </tr>
  </table>
</form>

这是Objective-C代码:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    if([request.URL.relativeString isEqualToString:@"http://localhost:8888/"])
    //if([request.URL.relativeString rangeOfString:@"http://localhost:8888/"].location)
    {
        //self.URL
        [self loadData];
        return true;
    }
   return true;
}

有人可以给我一个提示吗?谢谢!

在javascript中,使表单提交在javascript中设置一个变量,然后加载一个虚拟URL。

你在 Obj-C 中捕获该 url,就像你上面所做的那样,然后在 javascript 中调用一个函数来返回变量。

NSString *script = [NSString stringWithFormat:@"myfunc('%@')", html];
NSString *result = [self.webView stringByEvaluatingJavaScriptFromString:script];

所以 javascript 中的 myfunc 返回表单变量。