B2B网络软件

标题: AIWROK软件苹果IOS控件[WebView]方法小结 [打印本页]

作者: YYPOST群发软件    时间: 3 小时前
标题: AIWROK软件苹果IOS控件[WebView]方法小结
AIWROK软件苹果IOS控件[WebView]方法小结
AIWROK软件苹果IOS控件[WebView]方法小结 B2B网络软件 AIWROK软件苹果IOS控件[WebView]方法小结 B2B网络软件


  1. //🍎交流QQ群711841924
  2. //🍎WebView 控件[WebView]方法小结

  3. //第一个方法:📌show显示界面



  4. var web = new WebView()
  5. web.show();


  6. web.loadHtml(`

  7. <!DOCTYPE html>
  8. <html lang="en">
  9. < head >
  10.     < meta charset = "UTF-8" >
  11.     < title > WKWebView JS to Swift </ title >
  12.     < style >
  13.         body {
  14.             font - family: -apple - system, BlinkMacSystemFont, sans - serif;
  15.             padding: 40px;
  16.             background - color: #f2f2f7;
  17.             text - align: center;
  18.         }
  19.     button {
  20.             font-size: 18px;
  21.             padding: 12px 24px;
  22.             margin: 10px;
  23.             border: none;
  24.             border-radius: 8px;
  25.             background-color: #007aff;
  26.             color: white;
  27.             cursor: pointer;
  28.         }
  29. button: hover {
  30.     background - color: #005fd1;
  31.         }
  32.     </ style >
  33. </ head >
  34. < body >

  35.     < h1 > Swift 调用演示 </ h1 >
  36.    

  37.        < button onclick = "setConfig('a','6666')" > 设置值 </ button >
  38.    
  39.         < button onclick = "main()" > 加载脚本 </ button >
  40.      

  41.          < script >

  42.              async function main()
  43. {
  44.     //写入配置参数
  45.     setConfig('a', '6666')
  46.                   //获取配置参数
  47.              const result = await getConfig("a");
  48.     //吐司提示
  49.     toast(result.toString())
  50.      
  51.              //运行脚本代码,脚本要写到lamada表达式里
  52.     runJS(() => {
  53.                  //脚本写这里
  54.                  printl("1233")
  55.              })
  56.      
  57.              //运行脚本文件
  58.     runFile("主脚本.js")
  59.      

  60.         }


  61.     </ script >

  62. </ body >
  63. </ html >


  64. `)






  65. //第二个方法:&#128204;loadFile 加载本地文件


  66. new WebView().loadFile('')


  67. //第三个方法:&#128204;loadHtml 加载 HTML内容


  68. new WebView().loadHtml('')




  69. //第四个方法:&#128204;dismiss关闭界面

  70. new WebView().dismiss()




  71. //第五个方法:&#128204;loadUrl加载网页URL

  72. new WebView().loadUrl('')
复制代码
&#128204;show显示界面
类别
详情说明
方法功能
显示界面
方法签名
Void show()
返回值
Void
参数
案例
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 > 设置值 </ button >
   
        < button > 加载脚本 </ 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 >
`)
&#128204;loadFile 加载本地文件
类别
详情说明
方法功能
加载本地文件
方法签名
Void loadFile(String path)
返回值
Void
参数
- String path
:文件路径
案例
var fileLoader = new FileLoader();
fileLoader.loadFile("/sdcard/test.txt"); // 加载指定路径的本地文件
&#128204;loadHtml 加载 HTML内容
类别
详情说明
方法功能
加载 HTML 内容
方法签名
Void loadHtml(String html)
返回值
Void
参数
- String html
:HTML 字符串
案例
var webView = new WebView();
webView.loadHtml("<h1>Hello</h1>"); // 加载 HTML 内容
&#128204;dismiss关闭界面
类别
详情说明
方法功能
关闭界面
方法签名
Void dismiss()
返回值
Void
参数
案例
var window = new Window();
window.dismiss(); // 关闭该界面
&#128204;loadUrl加载网页URL
类别
详情说明
方法功能
加载网页 URL
方法签名
Void loadUrl(String url)
返回值
Void
参数
- String url
:网址字符串
案例
var webView = new WebView();
webView.loadUrl("https://www.example.com"); // 加载指定网址的网页
完整演示例子:


  1. //&#127822;交流QQ群711841924
  2. // 创建一个完整的 WebView 示例
  3. function createWebViewExample() {
  4.     // 创建 WebView 实例
  5.     var web = new WebView();
  6.    
  7.     // 显示界面
  8.     web.show();
  9.    
  10.     // 加载 HTML 冣容
  11.     web.loadHtml(`
  12.     <!DOCTYPE html>
  13.     <html lang="zh-CN">
  14.     <head>
  15.         <meta charset="UTF-8">
  16.         <meta name="viewport" content="width=device-width, initial-scale=1.0">
  17.         <title>WebView 控件示例</title>
  18.         <style>
  19.             body {
  20.                 font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  21.                 padding: 20px;
  22.                 background-color: #f5f5f5;
  23.                 text-align: center;
  24.             }
  25.             .container {
  26.                 max-width: 600px;
  27.                 margin: 0 auto;
  28.                 background: white;
  29.                 border-radius: 10px;
  30.                 padding: 30px;
  31.                 box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  32.             }
  33.             h1 {
  34.                 color: #333;
  35.                 margin-bottom: 30px;
  36.             }
  37.             button {
  38.                 font-size: 16px;
  39.                 padding: 12px 24px;
  40.                 margin: 10px;
  41.                 border: none;
  42.                 border-radius: 6px;
  43.                 background-color: #007aff;
  44.                 color: white;
  45.                 cursor: pointer;
  46.                 transition: background-color 0.3s;
  47.             }
  48.             button:hover {
  49.                 background-color: #005fd1;
  50.             }
  51.             button.secondary {
  52.                 background-color: #34c759;
  53.             }
  54.             button.secondary:hover {
  55.                 background-color: #2da44b;
  56.             }
  57.             button.warning {
  58.                 background-color: #ff9500;
  59.             }
  60.             button.warning:hover {
  61.                 background-color: #e08400;
  62.             }
  63.             .result {
  64.                 margin: 20px 0;
  65.                 padding: 15px;
  66.                 background-color: #f0f0f0;
  67.                 border-radius: 5px;
  68.                 min-height: 20px;
  69.             }
  70.         </style>
  71.     </head>
  72.     <body>
  73.         <div class="container">
  74.             <h1>WebView 控件功能演示</h1>
  75.             
  76.             <div>
  77.                 <button onclick="testSetConfig()">设置配置</button>
  78.                 <button onclick="testGetConfig()">获取配置</button>
  79.             </div>
  80.             
  81.             <div>
  82.                 <button class="secondary" onclick="testToast()">显示提示</button>
  83.                 <button class="secondary" onclick="testRunJS()">运行JS代码</button>
  84.             </div>
  85.             
  86.             <div>
  87.                 <button class="warning" onclick="loadExternalUrl()">加载外部网页</button>
  88.                 <button class="warning" onclick="loadLocalFile()">加载本地文件</button>
  89.             </div>
  90.             
  91.             <button style="background-color:#ff3b30" onclick="closeWebView()">关闭界面</button>
  92.             
  93.             <div class="result" id="result">
  94.                 操作结果将显示在这里
  95.             </div>
  96.         </div>

  97.         <script>
  98.             // 显示操作结果
  99.             function showResult(message) {
  100.                 document.getElementById('result').innerText = message;
  101.             }
  102.             
  103.             // 设置配置示例
  104.             function testSetConfig() {
  105.                 setConfig('username', '张三');
  106.                 setConfig('lastLogin', new Date().toString());
  107.                 showResult('配置已设置: username=张三');
  108.             }
  109.             
  110.             // 获取配置示例
  111.             async function testGetConfig() {
  112.                 try {
  113.                     const username = await getConfig('username');
  114.                     showResult('获取到配置: username=' + username);
  115.                 } catch (error) {
  116.                     showResult('获取配置失败: ' + error);
  117.                 }
  118.             }
  119.             
  120.             // 显示提示示例
  121.             function testToast() {
  122.                 toast('这是一条提示信息!');
  123.                 showResult('已显示提示信息');
  124.             }
  125.             
  126.             // 运行JS代码示例
  127.             function testRunJS() {
  128.                 runJS(() => {
  129.                     console.log('这是在原生环境中运行的JS代码');
  130.                     // 这里可以放置需要在原生环境中执行的脚本
  131.                     printl('WebView示例: JS代码已执行');
  132.                 });
  133.                 showResult('JS代码已在原生环境中运行');
  134.             }
  135.             
  136.             // 加载外部网页
  137.             function loadExternalUrl() {
  138.                 // 注意:在实际应用中,这里会跳转到指定网址
  139.                 showResult('在实际应用中将加载 https://www.baidu.com');
  140.             }
  141.             
  142.             // 加载本地文件
  143.             function loadLocalFile() {
  144.                 // 注意:在实际应用中,这里会加载指定的本地文件
  145.                 showResult('在实际应用中将加载本地文件 /sdcard/example.html');
  146.             }
  147.             
  148.             // 关闭界面
  149.             function closeWebView() {
  150.                 // 注意:在实际应用中,这会关闭WebView界面
  151.                 showResult('在实际应用中将关闭界面');
  152.             }
  153.         </script>
  154.     </body>
  155.     </html>
  156.     `);
  157.    
  158.     return web;
  159. }

  160. // 演示 WebView 的各种方法
  161. function demonstrateWebViewMethods() {
  162.     printl('开始演示 WebView 控件的各种方法');
  163.    
  164.     // 1. 创建并显示 WebView
  165.     var webView = createWebViewExample();
  166.     printl('1. WebView 已创建并显示');
  167.    
  168.     // 2. 演示 loadUrl 方法(注释形式,避免实际跳转)
  169.     // webView.loadUrl('https://www.baidu.com');
  170.     printl('2. loadUrl 方法可用于加载网页 URL');
  171.    
  172.     // 3. 演示 loadFile 方法(注释形式)
  173.     // webView.loadFile('/sdcard/index.html');
  174.     printl('3. loadFile 方法可用于加载本地文件');
  175.    
  176.     // 4. 演示 dismiss 方法(注释形式,避免实际关闭)
  177.     // webView.dismiss();
  178.     printl('4. dismiss 方法可用于关闭界面');
  179.    
  180.     printl('WebView 控件方法演示完成');
  181. }

  182. // 运行示例
  183. demonstrateWebViewMethods();
复制代码








欢迎光临 B2B网络软件 (http://bbs.niubt.cn/) Powered by Discuz! X3.2