 | |  |  | AIWROK软件IOS如何使用webview 实现 h5界面ui加载html代码
- var web = new WebView()
- web.show();
- web.loadHtml(`
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <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()">测试H5交互</button>
- <script>
- //执行脚本必须放到异步函数中 否则会卡顿
- async function main(){
- //写入配置参数
- setConfig('a','6666')
- //获取配置参数
- const result = await getConfig("a");
- //吐司提示
- toast(result.toString())
- //通过printl打印日志
- printl("测试日志打印")
- //调用脚本函数 第一个参数函数名,第二个要传的参数必须字符串,多个值可以用json
- const funres = await callFunction("test","123")
- //运行脚本代码,脚本要写到lamada表达式里
- runJS( ()=>{
- //AIWORK脚本写这里
- printl("1233")
- })
- //运行脚本文件
- //runFile("主脚本.js")
-
- }
-
- </script>
- </body>
- </html>
- `)
- function test(arg){
- printl("我被H5调用了,参数是"+arg)
- }
复制代码案例加载html文件 var web = new WebView()web.show();//这里从代码目录加载web.loadFile(project.getCodePath()+"h5.html") JS调用AIWORK方法: - setConfig('a','6666') 和 getConfig("a") 读写配置,getConfig返回的是一个对象,如果写入的是JSON格式的数据,需要使用JSON.stringify()转成字符串,查看字符串对象
- toast("123") 显示吐司信息
- runJS 函数用于运行脚本代码
- runFile("主脚本.js")运行脚本文件记得放到代码目录
| |  | |  |
|