feat(webview): 添加 onPageError 回调,支持页面加载失败检测
- XWebViewConfig 新增 onPageError(errorCode, description, failingUrl) 回调 - WebViewClient.onReceivedError 捕获主框架加载错误并回调 - App 层可据此显示重试 UI
这个提交包含在:
父节点
d015fd7d40
当前提交
aeeba4fac8
@ -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() {
|
||||
|
||||
正在加载...
在新工单中引用
屏蔽一个用户