Phase 1.8:WebViewJavascriptBridge.js 协议源码入 Bundle
H5 端桥协议实现,与 BridgeBus 的消息格式 1:1 对齐:
H5 → Native 走 window.webkit.messageHandlers.WVJBHandler.postMessage(...),
Native → H5 走 window.WebViewJavascriptBridge._handleMessageFromObjC(base64)。
- 新增 ylgamehall/Resources/JS/WebViewJavascriptBridge.js(~130 行)
- bridge.registerHandler(name, fn):H5 注册等待 Native callHandler
- bridge.callHandler(name, data, responseCallback):H5 主动调 Native
- bridge._handleMessageFromObjC(b64):Native 入口,base64 → JSON parse
→ 派发到 H5 handler 或匹配响应 responseCallbacks
- base64 解码用 decodeURIComponent + atob 处理 UTF-8 多字节字符
- 兼容 marcuswestin 旧式 window.WVJBCallbacks 队列握手:注入后
立即 flush 队列(H5 业务代码无需修改)
- RootViewController 加 Phase 1.8 烟雾测试:Bundle.main.url 找到 JS 文件
并打印 bytes / head;synchronized group 会把 Resources/JS/ 子目录
扁平化到 bundle 根,注释里说明此约束
- BuildProject 通过,bundle/WebViewJavascriptBridge.js 实际存在
- 下一步 Phase 1.9 BridgedWebView 用 WKUserScript(.atDocumentStart) 注入
本文件到所有 H5 页面,桥真正生效
This commit is contained in:
@@ -56,6 +56,22 @@ final class RootViewController: UIViewController {
|
||||
print("[ResourceUnzipper] ERROR: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 1.8 烟雾测试:确认 WebViewJavascriptBridge.js 已入 Bundle 且可读
|
||||
// 注:synchronized group 会把 ylgamehall/Resources/JS/ 子目录扁平化为 bundle 根,
|
||||
// 所以无需 subdirectory 参数。
|
||||
if let url = Bundle.main.url(forResource: "WebViewJavascriptBridge",
|
||||
withExtension: "js"),
|
||||
let content = try? String(contentsOf: url, encoding: .utf8) {
|
||||
print("""
|
||||
[WVJB.js] 已就绪
|
||||
path = \(url.path)
|
||||
bytes = \(content.utf8.count)
|
||||
head = \(content.prefix(60).replacingOccurrences(of: "\n", with: " ⏎ "))…
|
||||
""")
|
||||
} else {
|
||||
print("[WVJB.js] ERROR: 找不到 Bundle 内 JS/WebViewJavascriptBridge.js")
|
||||
}
|
||||
}
|
||||
|
||||
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
|
||||
|
||||
Reference in New Issue
Block a user