Files
youle_app_ios_v2/ylgamehall/Source/WebView/BridgedWebView.swift
T
joywayerandClaude Opus 4.7 a1c5883cc5 Phase 9.2:H5ErrorRelay 把 H5 异常中继到原生 print
零修改 H5(原则 A):WKUserScript .atDocumentStart 在业务 JS 跑前 hook
三类异常源:
- `console.error(...)`:保留原函数链式调用 + 转发 args
- `window.onerror`:onmessage / filename / lineno / colno / error.stack
- `unhandledrejection`:reason + reason.stack

走独立的 webkit.messageHandlers.h5error 通道(与 WVJB 不同 channel,不冲突)。
原生侧 MessageProxy weak target 切断 retain cycle。仅 BridgedWebView 安装
(大厅+子游戏),OverlayViewController 是第三方外链,不挂。

开发期价值:H5 出错时 Xcode console 直接能看到 "[H5 onerror] ... at xxx.js:42:8",
减少调试盲点。生产期 print 走 OSLog,对体积/性能影响极小。

新增:
- Source/Bridge/H5ErrorRelay.swift(@MainActor singleton)

接入:
- BridgedWebView.init:H5ErrorRelay.shared.install(into:) 在 WVJB 之后

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-23 02:04:46 +08:00

95 lines
4.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// BridgedWebView.swift
// ylgamehall
//
// WKWebView + WebViewJavascriptBridge.js 注入 + BridgeBus 一体封装。
// 详见 docs/H5-Native-Implementation-Design.md §3.3 / 契约 §4.1。
//
import UIKit
import WebKit
/// 内嵌 H5 的 WebView 组件,已挂好桥与必要配置。WebContainerViewControllerPhase 1.10
/// 通过把本 view 嵌入自己的内容区,再加上 16:9 letterbox / 生命周期管理。
@MainActor
public final class BridgedWebView: UIView {
public let webView: WKWebView
public let bridge: BridgeBus
public init() {
// ── WKWebViewConfiguration(契约 §4.1)─────────────────
let configuration = WKWebViewConfiguration()
configuration.defaultWebpagePreferences.allowsContentJavaScript = true
configuration.preferences.javaScriptCanOpenWindowsAutomatically = false
configuration.preferences.minimumFontSize = 10
configuration.allowsInlineMediaPlayback = true
configuration.processPool = SharedProcessPool.shared
// ── 在 documentStart 注入 WebViewJavascriptBridge.js ─────
// .atDocumentStart 保证 H5 业务代码运行时 window.WebViewJavascriptBridge 已就绪
if let url = Bundle.main.url(forResource: "WebViewJavascriptBridge",
withExtension: "js"),
let source = try? String(contentsOf: url, encoding: .utf8) {
let userScript = WKUserScript(
source: source,
injectionTime: .atDocumentStart,
forMainFrameOnly: true
)
configuration.userContentController.addUserScript(userScript)
} else {
// 编译期保证文件存在;运行时若缺,留 print 便于排查
print("[BridgedWebView] ERROR: WebViewJavascriptBridge.js 未在 Bundle 找到")
}
// ── H5 错误中继(Phase 9.2,原则 A 零修改 H5)─────────────
// 把 console.error / window.onerror / unhandledrejection 通过独立
// 的 webkit.messageHandlers.h5error 桥到原生 print,开发期减少盲点。
H5ErrorRelay.shared.install(into: configuration.userContentController)
// ── 创建 WKWebView + BridgeBus ─────────────────────────
let webView = WKWebView(frame: .zero, configuration: configuration)
let bridge = BridgeBus(webView: webView,
controller: configuration.userContentController)
self.webView = webView
self.bridge = bridge
super.init(frame: .zero)
// ── ScrollView 配置(契约 §4.1,禁手势滚动 / 弹性 / 自动 inset)──
let scroll = webView.scrollView
scroll.bounces = false
scroll.isScrollEnabled = false // ⚠️ 漏则 H5 上下滑动失控(契约硬约束)
scroll.contentInsetAdjustmentBehavior = .never
scroll.showsVerticalScrollIndicator = false
scroll.showsHorizontalScrollIndicator = false
// ── 布局:WebView 撑满 self ─────────────────────────────
webView.translatesAutoresizingMaskIntoConstraints = false
addSubview(webView)
NSLayoutConstraint.activate([
webView.topAnchor.constraint(equalTo: topAnchor),
webView.bottomAnchor.constraint(equalTo: bottomAnchor),
webView.leadingAnchor.constraint(equalTo: leadingAnchor),
webView.trailingAnchor.constraint(equalTo: trailingAnchor)
])
backgroundColor = .black
}
@available(*, unavailable)
public required init?(coder: NSCoder) {
fatalError("BridgedWebView does not support init(coder:)")
}
}
// MARK: - 跨 WebView 共享的进程池
/// 大厅 / 子游戏 / 弹层共用同一 WKProcessPool,共享 Cookie / 资源缓存,
/// 启动加速。详见 Design §3.5。
@MainActor
public enum SharedProcessPool {
public static let shared = WKProcessPool()
}