uniapp? JSBridge? ???? ????? ?? ???? ??? ????, ???? ?? ?? ??? ?????
1. ?? ??
??? ?????? ????? ??? ?? ???? ??? ???? ? ???? ??? ?? ???? ???. ?? ?? ???? ????. ??? ??? ??? ?????? ?? ?????? uniapp? JSBridge? ???? ?????? ?? ??? ?? ??? ? ?? ??? ??? ?????.
JSBridge? ??? ??? ???? ??? ?? ???? ???? ??? ??? ???? ???? ???? ???? ???? ?? ? ??? ?? ?? ??????. ??? ?? ?? ??? ??? ?? ??? ??? ??? ?? ????.
2. JSBridge
- ?? ?? uniapp ????? ? js ??? ??? ??? JSBridge.js? ?????. ? ??? ??? ??? ?? ?? ?? ??? ??? ??? ???.
- ??? ??? ???? ??? ???? ?? ??? ????? JSBridge.js ??? ?? ??? ?????. ?? ??? ??? ????.
// JSBridge.js let messageHandlers = {}; // 存儲(chǔ)前端和原生之間的消息和回調(diào)函數(shù) // 注冊消息處理函數(shù),前端通過調(diào)用此函數(shù)來注冊對應(yīng)的回調(diào)函數(shù) function registerHandler(name, handler) { messageHandlers[name] = handler; } // 發(fā)送消息到原生 function sendMessageToNative(name, data, callback) { let message = { name: name, data: data }; // 注冊回調(diào)函數(shù) if (callback) { let callbackId = 'cb_' + Date.now(); message.callbackId = callbackId; messageHandlers[callbackId] = callback; } // 向原生發(fā)送消息 window.webkit.messageHandlers[name].postMessage(message); } // 處理原生發(fā)送過來的消息 function handleMessageFromNative(message) { let handler = messageHandlers[message.name]; if (handler) { handler(message.data, function(response) { sendMessageToNative(message.callbackId, response); // 發(fā)送回調(diào)消息給原生 }); } } window.messageHandlers = messageHandlers; window.registerHandler = registerHandler; window.sendMessageToNative = sendMessageToNative; window.handleMessageFromNative = handleMessageFromNative;
- uniapp ?????
main.js
??? JSBridge.js? ???? ??? ?? ??? ?????.main.js
文件中引入JSBridge.js,并注冊消息處理函數(shù),示例代碼如下:
// main.js import JSBridge from './JSBridge.js'; // 注冊消息處理函數(shù),前端通過調(diào)用此函數(shù)來注冊對應(yīng)的回調(diào)函數(shù) JSBridge.registerHandler('getUserInfo', function(data, callback) { console.log('前端收到getUserInfo消息:', data); // 假設(shè)需要獲取用戶信息,可以通過uniapp的API來實(shí)現(xiàn) let userInfo = uni.getUserInfo(); // 返回獲取到的用戶信息給原生 callback(userInfo); }); // 假設(shè)頁面上有一個(gè)按鈕,點(diǎn)擊按鈕時(shí)調(diào)用原生的方法 document.getElementById('btn').addEventListener('click', function() { // 發(fā)送消息到原生 JSBridge.sendMessageToNative('showAlert', { title: 'Hello', message: 'World' }); });
- 在原生環(huán)境中實(shí)現(xiàn)與前端交互的功能和邏輯。示例代碼如下:
// 在iOS原生代碼中 import WebKit class ViewController: UIViewController { var webView: WKWebView! override func viewDidLoad() { super.viewDidLoad() // 創(chuàng)建WebView webView = WKWebView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)) view.addSubview(webView) // 加載uniapp的HTML文件 if let url = Bundle.main.url(forResource: "uniapp", withExtension: "html") { webView.loadFileURL(url, allowingReadAccessTo: url) } // 注冊JSBridge處理函數(shù),用于處理前端發(fā)送來的消息 webView.configuration.userContentController.add(self, name: "getUserInfo") webView.configuration.userContentController.add(self, name: "showAlert") } } extension ViewController: WKScriptMessageHandler { func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { if let body = message.body as? [String: Any] { let name = message.name if name == "getUserInfo" { print("原生收到getUserInfo消息:", body) // TODO: 獲取原生的用戶信息 // 返回用戶信息給前端 let userInfo = [ "name": "John", "age": 20 ] let response = [ "data": userInfo ] let javascript = "window.handleMessageFromNative((response))" webView.evaluateJavaScript(javascript, completionHandler: nil) } else if name == "showAlert" { print("原生收到showAlert消息:", body) // 假設(shè)實(shí)現(xiàn)一個(gè)彈窗功能 let title = body["title"] as? String ?? "" let message = body["message"] as? String ?? "" let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) present(alertController, animated: true, completion: nil) } } } }
三、使用JSBridge進(jìn)行前端與原生交互
通過上述的步驟,我們已經(jīng)實(shí)現(xiàn)了基本的JSBridge橋梁和消息處理函數(shù)。在前端代碼中,我們可以調(diào)用JSBridge.sendMessageToNative()
方法向原生發(fā)送消息,同時(shí)也可以注冊對應(yīng)的消息處理函數(shù),如示例中的JSBridge.registerHandler()
。在原生代碼中,我們通過userContentController.add()
方法注冊處理函數(shù),用于接收前端發(fā)送的消息,并實(shí)現(xiàn)相應(yīng)的功能。
在頁面中,當(dāng)點(diǎn)擊按鈕時(shí),調(diào)用JSBridge.sendMessageToNative('showAlert', { title: 'Hello', message: 'World' })
方法發(fā)送消息到原生,原生接收到消息后,彈出一個(gè)帶有標(biāo)題和內(nèi)容的彈窗。另外,當(dāng)前端需要獲取用戶信息時(shí),調(diào)用JSBridge.sendMessageToNative('getUserInfo')
?????? ?????? ??? ??? ???? ???? ??????. ?? ??? ??? ????.
rrreee
3. ??? ?? ? ???? ?? ??? ?? JSBridge? ?????. ????? ??? ?? ?? JSBridge ??? ? ??? ?? ??? ??????. ????? ????JSBridge.sendMessageToNative()
???? ???? ????? ???? ?? ? ??? JSBridge.registerHandler()? ?? ?? ??? ?? ??? ??? ?? ????.
????. ???? ????? userContentController.add()
???? ?? ??? ???? ?? ???? ???? ?? ??? ???? ?? ??? ?????. ????????? ??? ???? JSBridge.sendMessageToNative('showAlert', { title: 'Hello', message: 'World' })
???? ???? ???? ???? ??? ??? ??? ?? ???? ???. ?? ??????? ??? ??? ???? ?? ?? JSBridge.sendMessageToNative('getUserInfo')
???? ???? ????? ???? ????. ?????? ?? ??? ?? ???? ??? ?????. ????????? JSBridge? ???? uniapp? ???? ?? ?? ?? ??? ?? ??? ? ??? ?? ??? ??? ?????? ?? ??? ??? ??? ? ????. ??? ?? ??? ???? ???? ???? ???? ??? ? ????. ????? ??? JSBridge? ???? ???? ????? ???? uniapp? ?? ??? ??? ?? ?????. ??? ??? uniapp ??? ??? JSBridge? ???? ????? ?? ??????? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

UniApp? ??? ??? ?? ??????? ?? ???? ??? ??? ??? ?????. ????? ?? ??? ?? ??? ???? ?? ??, ??? ??? ? ??? ??? ?? ????. ???? ????? ?? ??? ????? ?????? ?? ???? ??? ?? ??? ?????. ??? ?????? ??? ??? ?? ??? ??? ???? ?? API ??? ???? ????. WebView? ?? ????? ?? ??????? ???? ?????? ??? ??? ? ????. ??? ?? ???? ???? ?????? ??? ? ?????? ?? ???? ???? ????? ?? ? ?? ?? ??? ?????.

UniApp? Vue.js? ???? ?? Flutter? Dart? ???? ?? ? ? ??? ??? ??? ?????. UniApp? ??? ?? ??? ?? ??? ????? WebView? ?? ??? ?????. Flutter? ??? ??? ?? ?? ??? ??? ????? ??? ? ?????. UniApp?? ??? ?? ????? ?? Flutter?? ??? ??? ????? ????. UniApp? ?? ??? ?? ?? ?? ??? ?? ????? ?????. Flutter? ?? ??? ?? ? ???? ?? ??? ??????? ?????.

WebStorm?? UniApp ???? ????? ???? ??: UniApp ?? ?? ???? ?? ?? ??? ?? WebSocket ?? ????

????? ??? ?? ??? ??? ?? uni-app? ? ??, ????? ??? ???? ?????? ??? ?? MUI? ? ????. ?? uni-app?? 1. Vue.js/JavaScript ?? 2. ??? ?? ?? ??/API 3. ?? ???? ????. ??? ??? ????. 1. ?? ?? 2. ????? ??? ??? ?????. MUI?? ??? ?????. 1. ???? ??? ?? 2. ?? ??? 3. ???? ?? ??/?? ?????. ??? ??? ????. 1. CSS ??? 2. ?? ?? ??? ???? ????. 3. ??? ???.

UniApp ?? ?? ??: 'xxx' ????? ??? ?? ? ????. UniApp? Vue.js ?????? ???? ?? ??? ??? ?????? ?? ?????? WeChat ???, H5 ? ?? ???? ?? ?? ???? ??????? ???? ? ??? ? ????. ?. ?? ???? ??? ??? ??? ????? ?? ????? ??? ?? ?????. ??? ??? ??? ??? ? ????: 'xxx' ????? ??? ?? ? ????. ? ??? ?? ?????? ????? ???? ?? ??? ??? ?? ? ????. ? ????? ? ??? ???? ?? ?? ??? ?????.

UniApp? ???? ?? ??? ??? ?? ?? ??, ??, ??? ?? ? ???? ???? ???. UniApp? ??? ??? ??? ??, ?? ??, ?? ?? ? ?? ??????, ???? ??? ??, ???, ???? ?? ? ???? ?????. ???? ???? ????? ?? ???? ?????. UniApp? ????? ????, ???? ??? ???? ??? ??? ???? ??? ??????? ?????.

win11 ?????? ?? ?? ?? ??? ?? ?? ???? ??? ???? ???? ?? ??? ? ????. ??? ?? ???? ??? ?? ?? ?? ??? ?? ??? ????. ??? ???? ???? ????. win11?? ?? ?? ?? ??? ?? ?? 1. ?? ??? ???? "??"? ????. 2. ?? ?? ???? "???" ??? ????. 3. ??? ??? ??? ? ??? "?????"? ?????. 4. ?? ?? ???? ?? ??????? "? ????? ??"? ?????.
