feat(webview): 添加 onPageError 回调,支持页面加载失败检测

- XWebViewConfig 新增 onPageError(errorCode, description, failingUrl) 回调
- WebViewClient.onReceivedError 捕获主框架加载错误并回调
- App 层可据此显示重试 UI
这个提交包含在:
XuqmGroup 2026-06-18 13:43:56 +08:00
父节点 d015fd7d40
当前提交 aeeba4fac8
共有 2 个文件被更改,包括 15 次插入0 次删除

查看文件

@ -16,6 +16,8 @@ data class XWebViewConfig(
/** When non-null, shows a status-bar progress notification with this title while downloading. */
val downloadNotificationTitle: String? = null,
val onMessage: ((String) -> Unit)? = null,
/** 页面加载失败时回调。errorCode / description / failingUrl 对应 WebViewClient.onReceivedError 参数。 */
val onPageError: ((errorCode: Int, description: String, failingUrl: String) -> Unit)? = null,
)
interface XWebViewController {

查看文件

@ -16,6 +16,7 @@ import android.webkit.MimeTypeMap
import android.webkit.PermissionRequest
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebViewClient
import android.webkit.WebView
@ -532,6 +533,18 @@ fun XWebViewView(
openExternalScheme(ctx, uri)
return true
}
override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
super.onReceivedError(view, request, error)
// 仅处理主框架加载错误忽略子资源如图片、JS 等)
if (request?.isForMainFrame == true) {
val code = error?.errorCode ?: -1
val desc = error?.description?.toString() ?: "Unknown error"
val url = request.url?.toString() ?: ""
android.util.Log.e("XWV", "onReceivedError: code=$code, desc=$desc, url=$url")
mainHandler.post { config.onPageError?.invoke(code, desc, url) }
}
}
}
webChromeClient = object : WebChromeClient() {