AIWROKÈí¼þÆ»¹ûIOS¿Ø¼þ[WebView]·½·¨Ð¡½á
📌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 > `) |
📌loadFile ¼ÓÔØ±¾µØÎļþ Àà±ð | ÏêÇé˵Ã÷ | ·½·¨¹¦ÄÜ | ¼ÓÔØ±¾µØÎļþ | ·½·¨Ç©Ãû | Void loadFile(String path) | ·µ»ØÖµ | Void | ²ÎÊý | - String path £ºÎļþ·¾¶ | °¸Àý | var fileLoader = new FileLoader(); fileLoader.loadFile("/sdcard/test.txt"); // ¼ÓÔØÖ¸¶¨Â·¾¶µÄ±¾µØÎļþ |
📌loadHtml ¼ÓÔØ HTMLÄÚÈÝ Àà±ð | ÏêÇé˵Ã÷ | ·½·¨¹¦ÄÜ | ¼ÓÔØ HTML ÄÚÈÝ | ·½·¨Ç©Ãû | Void loadHtml(String html) | ·µ»ØÖµ | Void | ²ÎÊý | - String html £ºHTML ×Ö·û´® | °¸Àý | var webView = new WebView(); webView.loadHtml("<h1>Hello</h1>"); // ¼ÓÔØ HTML ÄÚÈÝ |
📌dismiss¹Ø±Õ½çÃæ Àà±ð | ÏêÇé˵Ã÷ | ·½·¨¹¦ÄÜ | ¹Ø±Õ½çÃæ | ·½·¨Ç©Ãû | Void dismiss() | ·µ»ØÖµ | Void | ²ÎÊý | ÎÞ | °¸Àý | var window = new Window(); window.dismiss(); // ¹Ø±Õ¸Ã½çÃæ |
📌loadUrl¼ÓÔØÍøÒ³URL Àà±ð | ÏêÇé˵Ã÷ | ·½·¨¹¦ÄÜ | ¼ÓÔØÍøÒ³ URL | ·½·¨Ç©Ãû | Void loadUrl(String url) | ·µ»ØÖµ | Void | ²ÎÊý | - String url £ºÍøÖ·×Ö·û´® | °¸Àý | var webView = new WebView(); webView.loadUrl("https://www.example.com"); // ¼ÓÔØÖ¸¶¨ÍøÖ·µÄÍøÒ³ |
ÍêÕûÑÝʾÀý×Ó£º
- //🍎½»Á÷QQȺ711841924
- // ´´½¨Ò»¸öÍêÕûµÄ WebView ʾÀý
- function createWebViewExample() {
- // ´´½¨ WebView ʵÀý
- var web = new WebView();
-
- // ÏÔʾ½çÃæ
- web.show();
-
- // ¼ÓÔØ HTML ƒâÈÝ
- web.loadHtml(`
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>WebView ¿Ø¼þʾÀý</title>
- <style>
- body {
- font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
- padding: 20px;
- background-color: #f5f5f5;
- text-align: center;
- }
- .container {
- max-width: 600px;
- margin: 0 auto;
- background: white;
- border-radius: 10px;
- padding: 30px;
- box-shadow: 0 2px 10px rgba(0,0,0,0.1);
- }
- h1 {
- color: #333;
- margin-bottom: 30px;
- }
- button {
- font-size: 16px;
- padding: 12px 24px;
- margin: 10px;
- border: none;
- border-radius: 6px;
- background-color: #007aff;
- color: white;
- cursor: pointer;
- transition: background-color 0.3s;
- }
- button:hover {
- background-color: #005fd1;
- }
- button.secondary {
- background-color: #34c759;
- }
- button.secondary:hover {
- background-color: #2da44b;
- }
- button.warning {
- background-color: #ff9500;
- }
- button.warning:hover {
- background-color: #e08400;
- }
- .result {
- margin: 20px 0;
- padding: 15px;
- background-color: #f0f0f0;
- border-radius: 5px;
- min-height: 20px;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <h1>WebView ¿Ø¼þ¹¦ÄÜÑÝʾ</h1>
-
- <div>
- <button onclick="testSetConfig()">ÉèÖÃÅäÖÃ</button>
- <button onclick="testGetConfig()">»ñÈ¡ÅäÖÃ</button>
- </div>
-
- <div>
- <button class="secondary" onclick="testToast()">ÏÔʾÌáʾ</button>
- <button class="secondary" onclick="testRunJS()">ÔËÐÐJS´úÂë</button>
- </div>
-
- <div>
- <button class="warning" onclick="loadExternalUrl()">¼ÓÔØÍâ²¿ÍøÒ³</button>
- <button class="warning" onclick="loadLocalFile()">¼ÓÔØ±¾µØÎļþ</button>
- </div>
-
- <button style="background-color:#ff3b30" onclick="closeWebView()">¹Ø±Õ½çÃæ</button>
-
- <div class="result" id="result">
- ²Ù×÷½á¹û½«ÏÔʾÔÚÕâÀï
- </div>
- </div>
- <script>
- // ÏÔʾ²Ù×÷½á¹û
- function showResult(message) {
- document.getElementById('result').innerText = message;
- }
-
- // ÉèÖÃÅäÖÃʾÀý
- function testSetConfig() {
- setConfig('username', 'ÕÅÈý');
- setConfig('lastLogin', new Date().toString());
- showResult('ÅäÖÃÒÑÉèÖÃ: username=ÕÅÈý');
- }
-
- // »ñÈ¡ÅäÖÃʾÀý
- async function testGetConfig() {
- try {
- const username = await getConfig('username');
- showResult('»ñÈ¡µ½ÅäÖÃ: username=' + username);
- } catch (error) {
- showResult('»ñÈ¡ÅäÖÃʧ°Ü: ' + error);
- }
- }
-
- // ÏÔʾÌáʾʾÀý
- function testToast() {
- toast('ÕâÊÇÒ»ÌõÌáʾÐÅÏ¢£¡');
- showResult('ÒÑÏÔʾÌáʾÐÅÏ¢');
- }
-
- // ÔËÐÐJS´úÂëʾÀý
- function testRunJS() {
- runJS(() => {
- console.log('ÕâÊÇÔÚÔÉú»·¾³ÖÐÔËÐеÄJS´úÂë');
- // ÕâÀï¿ÉÒÔ·ÅÖÃÐèÒªÔÚÔÉú»·¾³ÖÐÖ´ÐеĽű¾
- printl('WebViewʾÀý: JS´úÂëÒÑÖ´ÐÐ');
- });
- showResult('JS´úÂëÒÑÔÚÔÉú»·¾³ÖÐÔËÐÐ');
- }
-
- // ¼ÓÔØÍâ²¿ÍøÒ³
- function loadExternalUrl() {
- // ×¢Ò⣺ÔÚʵ¼ÊÓ¦ÓÃÖУ¬ÕâÀï»áÌø×ªµ½Ö¸¶¨ÍøÖ·
- showResult('ÔÚʵ¼ÊÓ¦ÓÃÖн«¼ÓÔØ https://www.baidu.com');
- }
-
- // ¼ÓÔØ±¾µØÎļþ
- function loadLocalFile() {
- // ×¢Ò⣺ÔÚʵ¼ÊÓ¦ÓÃÖУ¬ÕâÀï»á¼ÓÔØÖ¸¶¨µÄ±¾µØÎļþ
- showResult('ÔÚʵ¼ÊÓ¦ÓÃÖн«¼ÓÔØ±¾µØÎļþ /sdcard/example.html');
- }
-
- // ¹Ø±Õ½çÃæ
- function closeWebView() {
- // ×¢Ò⣺ÔÚʵ¼ÊÓ¦ÓÃÖУ¬Õâ»á¹Ø±ÕWebView½çÃæ
- showResult('ÔÚʵ¼ÊÓ¦ÓÃÖн«¹Ø±Õ½çÃæ');
- }
- </script>
- </body>
- </html>
- `);
-
- return web;
- }
- // ÑÝʾ WebView µÄ¸÷ÖÖ·½·¨
- function demonstrateWebViewMethods() {
- printl('¿ªÊ¼ÑÝʾ WebView ¿Ø¼þµÄ¸÷ÖÖ·½·¨');
-
- // 1. ´´½¨²¢ÏÔʾ WebView
- var webView = createWebViewExample();
- printl('1. WebView ÒÑ´´½¨²¢ÏÔʾ');
-
- // 2. ÑÝʾ loadUrl ·½·¨£¨×¢ÊÍÐÎʽ£¬±ÜÃâʵ¼ÊÌø×ª£©
- // webView.loadUrl('https://www.baidu.com');
- printl('2. loadUrl ·½·¨¿ÉÓÃÓÚ¼ÓÔØÍøÒ³ URL');
-
- // 3. ÑÝʾ loadFile ·½·¨£¨×¢ÊÍÐÎʽ£©
- // webView.loadFile('/sdcard/index.html');
- printl('3. loadFile ·½·¨¿ÉÓÃÓÚ¼ÓÔØ±¾µØÎļþ');
-
- // 4. ÑÝʾ dismiss ·½·¨£¨×¢ÊÍÐÎʽ£¬±ÜÃâʵ¼Ê¹Ø±Õ£©
- // webView.dismiss();
- printl('4. dismiss ·½·¨¿ÉÓÃÓڹرսçÃæ');
-
- printl('WebView ¿Ø¼þ·½·¨ÑÝʾÍê³É');
- }
- // ÔËÐÐʾÀý
- demonstrateWebViewMethods();
¸´ÖÆ´úÂë
|