如何使用Swift webView调用在函数中返回HTML字符串的JavaScript函数?

时间:2017-05-02 11:59:10

标签: javascript html ios swift uiwebview

我有一个HTML文件,它是在调用正文中的JavaScript函数后生成的。我需要从HTML中删除JavaScript调用,并使用webView委托从我的Swift中调用它。我尝试使用stringByevaluatingJavaScript但它不起作用。我正在添加包含JavaScript函数的HTML文件。

<!DOCTYPE html>
<html>
<head>
  <title>Complete Blood Count</title>
  <link href="https://www.fontify.me/wf/4dae5b9279578bbce15473cde2ae897f" rel="stylesheet" type="text/css">
  <style type="text/css">
    body {
      font-family: "font92727";
    }
    .main-box {
        width: 96%;
        display: inline-block;
        margin: 0 auto;
        padding: 0;
        position: absolute;
        font-size: 3em;
    }

    .image-box {
        display: inline-block;
    }

    .image-box img {
        width: 100%;
        height: inherit;
        display: inline-block;
    }

    .value-box {
    }

    .value-inner-box.wbc {
        position: absolute;
        left: 12%;
        top: 37%;
        text-align: center;
    }

    .value-inner-box.hbc {
        position: absolute;
        left: 46%;
        top: 15%;
        text-align: center;
    }

    .value-inner-box.hct {
        position: absolute;
        left: 46%;
        top: 60%;
        text-align: center;
    }

    .value-inner-box.plt {
        position: absolute;
        left: 82%;
        top: 37%;
        text-align: center;
    }

    @media screen and (max-width: 1920px) and (min-width: 1025px), (min-width: 2048px) {
        .main-box {
          font-size: 6em;
        }
    }
  </style>  
</head>
<body>
<script type="text/javascript">
    
    document.write(cbc({ wbc: 14.0, wbcCol:'red', hgb: 12.0, hbcCol:'orange', hct: 12.0, hctCol: 'blue', plt: 12.4, pltCol: 'green'}));

    function cbc(cbcdata) {
      var output = "";
        
      var wbc0 = cbcdata.wbc;
      var hgb0 = cbcdata.hgb;
      var hct0 = cbcdata.hct;
      var hct0 = cbcdata.plt;

      var wbcC0 = cbcdata.wbcCol;
      var hbcC0 = cbcdata.hbcCol;
      var hctC0 = cbcdata.hctCol;
      var pltC0 = cbcdata.pltCol;


      output = output + "<div class='main-box'>";
      output = output + "<div class='image-box'>";
      output = output + "<img src='cbc2x.png'>";
      output = output + "</div>";
      output = output + "<div class='value-box'>";
      output = output + "<div class='value-inner-box wbc'>";
      output = output + "<div class='vale-labellabel' style ='color:"+wbcC0+"'>WBC</div>";
      output = output + "<div class='value-text' style ='color:"+wbcC0+"'>" + wbc0 +"</div>";
      output = output + "</div>";
      output = output + "<div class='value-inner-box hbc'>";
      output = output + "<div class='vale-labellabel' style ='color:"+hbcC0+"'>HBC</div>";
      output = output + "<div class='value-text' style ='color:"+hbcC0+"'>" + hgb0 +"</div>";
      output = output + "</div>";
      output = output + "<div class='value-inner-box hct'>";
      output = output + "<div class='vale-labellabel' style ='color:"+hctC0+"'>HCT</div>";
      output = output + "<div class='value-text' style ='color:"+hctC0+"'>" + hct0 +"</div>";
      output = output + "</div>";
      output = output + "<div class='value-inner-box plt'>";
      output = output + "<div class='vale-labellabel' style ='color:"+pltC0+"'>PLT</div>";
      output = output + "<div class='value-text' style ='color:"+pltC0+"'>" + hct0 +"</div>";
      output = output + "</div>";
      output = output + "</div>";

      return output;
    } 

</script>


</body>
</html>

func webViewDidFinishLoad(webView: UIWebView) {
        
        if((javaScriptCalledDict.objectForKey(dataSourceArray![webView.tag] as! String) as! Bool) == false){
            let jsString = String(format:"cbc({ wbc: 14.0, wbcCol:'red', hgb: 12.0, hbcCol:'orange', hct: 12.0, hctCol: 'blue', plt: 12.4, pltCol: 'green'})")
            webView.stringByEvaluatingJavaScriptFromString(jsString)!
            javaScriptCalledDict.setObject(true, forKey: dataSourceArray![webView.tag] as! String)
        }
        
        
    }

这是使用传递给JS函数的固定值呈现的HTML。我需要使用我的参数集来调用JS函数。任何人都可以建议对HTML或我的快速代码进行任何更改。

1 个答案:

答案 0 :(得分:0)

您只需要更改:

let htmlReturned = webView.stringByEvaluatingJavaScriptFromString(jsString)!

这里jsString将等于你的javascript函数的名称。然而,这和webView的问题在于你无法将iOS代码中的对象值传递到javascript函数中(至少我知道,如果我们可以证明我错了:))....所以你会需要更改函数签名以接受字符串和数字

function cbc(wbc, wbcCol, hgb, hbcCol, hct, hctCol, plt, pltCol){}

最终会:

let htmlReturn = webview.stringByEvaluatingJavascriptFromString("cbc(14, \"red\", 12, \"orange\", 12, \"blue\", 12.4, \"green\")")