var web = new WebView()
web.show();
web.loadHtml(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WKWebView JS to Swift</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, sans-serif;
padding: 40px;
background-color: #f2f2f7;
text-align: center;
}
button {
font-size: 18px;
padding: 12px 24px;
margin: 10px;
border: none;
border-radius: 8px;
background-color: #007aff;
color: white;
cursor: pointer;
}
button:hover {
background-color: #005fd1;
}
</style>
</head>
<body>
<h1>Swift 调用演示</h1>
<button onclick="setConfig('a','6666')">设置值</button>
<button onclick="main()">加载脚本</button>
<script>
async function main(){
//写入配置参数
setConfig('a','6666')
//获取配置参数
const result = await getConfig("a");
//吐司提示
toast(result.toString())
//运行脚本代码,脚本要写到lamada表达式里
runJS( ()=>{
//脚本写这里
printl("1233")
})
//运行脚本文件
runFile("主脚本.js")
}
</script>
</body>
</html>
`)
setConfig('a','6666')
和 getConfig("a")
这两个函数在你的HTML代码中被调用,但并未在提供的代码中定义。你需要在Swift代码中定义这些函数,或者在HTML中添加相应的JavaScript实现。toast(result.toString())
函数在你的HTML代码中被调用,但并未在提供的代码中定义。同理,你需要在Swift代码中定义这个函数,或者在HTML中添加相应的JavaScript实现来显示吐司提示。runJS
函数用于运行JavaScript代码,你需要在Swift中实现这个函数,以允许WebView执行传入的JavaScript代码块。runFile("主脚本.js")
函数用于运行JavaScript文件,同样需要在Swift中实现,以允许WebView加载并执行指定的JavaScript文件。欢迎光临 B2B网络软件 (http://bbs.niubt.cn/) | Powered by Discuz! X3.2 |