gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:29:33 25 4
gpt4 key购买 nike

我有一个 HTML 文件,它是在正文中调用 JavaScript 函数后生成的。我需要从 HTML 中删除 JavaScript 调用,并使用 webView 委托(delegate)从我的 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 或我的 swift 代码进行任何更改。

最佳答案

您只需要更改:

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\")")

关于javascript - 如何使用 Swift webView 调用在正文中返回 HTML 字符串的 JavaScript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43737316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com