开始
复制一个包含安装步骤和此插件的完整Markdown指南的设置提示。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-webview-crash`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/webview-crash/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
标题为“安装”您可以使用我们的 AI 助手设置来安装插件。将 Capgo 技能添加到您的 AI 工具中,使用以下命令:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下提示:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-webview-crash` plugin in my project.如果您prefer手动设置,安装插件并运行以下命令并遵循以下平台特定说明:
npm install @capgo/capacitor-webview-crashnpx cap sync导入
导入import { WebViewCrash } from '@capgo/capacitor-webview-crash';推荐恢复流程
推荐恢复流程尽早在应用启动时附加监听器,以便恢复的运行时可以在用户继续浏览之前反应:
import { WebViewCrash } from '@capgo/capacitor-webview-crash';
await WebViewCrash.addListener('webViewRestoredAfterCrash', async (info) => { console.log('Recovered after a WebView crash', info);
// Rehydrate critical state, reopen the correct screen, or prompt the user to retry. await WebViewCrash.clearPendingCrashInfo();});
await WebViewCrash.addListener('webViewRestoredAfterRestart', async (info) => { console.log('Recovered after a native WebView restart', info); await WebViewCrash.clearPendingCrashInfo();});
const pending = await WebViewCrash.getPendingCrashInfo();// Note: the listener callback may have already cleared the pending marker.if (pending.value) { console.log('Pending crash or restart marker', pending.value);}原生自动重启
原生自动重启在 capacitor.config.ts 以便决策在JavaScript运行时崩溃或尚未加载时仍然在原生code中:
import type { CapacitorConfig } from '@capacitor/cli';import type { WebViewCrashPluginConfig } from '@capgo/capacitor-webview-crash';
const webViewCrash: WebViewCrashPluginConfig = { // Enabled by default. Keep this on for long-running apps. restartOnCrash: true,
// Use a 5-field cron schedule in the device local timezone. // Do not combine restartCron with an active restartIntervalMs. restartCron: '0 3 * * *',
// Optional delay before restarting after a crash. restartAfterCrashDelayMs: 0,};
const config: CapacitorConfig = { plugins: { WebViewCrash: webViewCrash, },};
export default config;使用 restartIntervalMs 适用于 restartCron 的应用程序,用户可能会在同一个 0 3 * * * 中保持打开状态几天: reason: 'periodicRestart', then Android recreates the host activity and iOS rebuilds the Capacitor bridge view so a new WKWebView is created from native code.
的应用程序,例如每天凌晨03:00在设备本地时间区重新启动。预定的重新启动会写入一个待处理标记, restartCron 重新创建宿主活动, *重建 restartCron 视图,因此一个新的 restartIntervalMs 从本机 0创建。
手动原生重启
手动原生重启调用 restartWebView() context:在Appflow比较/迁移营销复制中看到的Appflow迁移步骤2(Appflow Migration Step2)
await WebViewCrash.restartWebView();复制到剪贴板 reason: 'manualRestart', resolves the current call, then asks native code to create a fresh WebView. Android recreates the host activity. iOS rebuilds the Capacitor bridge view so a new WKWebView , 解决当前调用,然后要求原生__CAPGO_KEEP_0__创建一个新的WebView。Android重新创建宿主活动。iOS重建__CAPGO_KEEP_1__桥接视图,因此一个新的
API overview
API概述getPendingCrashInfo
__CAPGO_KEEP_0__概述获取待处理崩溃信息 null 当没有任何待处理的异常时。
const pending = await WebViewCrash.getPendingCrashInfo();if (pending.value) { console.log(pending.value.platform, pending.value.reason);}clearPendingCrashInfo
标题:清除待处理的异常信息在处理恢复后清除存储的标记。
await WebViewCrash.clearPendingCrashInfo();simulateCrashRecovery
标题:模拟异常恢复创建一个虚拟的异常标记,以便 QA 和本地调试可以在不崩溃真实 WebView 的情况下演练恢复路径。
const simulated = await WebViewCrash.simulateCrashRecovery();console.log(simulated.value);restartWebView
标题:重启 WebView存储一个手动重启标记,并要求本机 code 创建一个新的 WebView。
await WebViewCrash.restartWebView();平台说明
Platform notes- Android从
onRenderProcessGone包括didCrash和rendererPriorityAtExit当平台提供时 - iOS从
webViewWebContentProcessDidTerminate并在可用时添加当前应用程序状态 - 手动和预定重启创建一个新的WebView。Android重新创建宿主活动;iOS重建Capacitor桥接视图。
- 预定重启使用
reason: 'periodicRestart'手动重启使用reason: 'manualRestart'. - Web无法检测真实的渲染器崩溃。Web实现仅模拟本地存储的行为。
类型参考
类型参考PendingCrashInfoResult
PendingCrashInfoResultexport interface PendingCrashInfoResult { /** * Stored crash or restart metadata, or `null` when no marker is pending. */ value: WebViewCrashInfo | null;}WebViewCrashPluginConfig
WebViewCrashPlugin配置export interface WebViewCrashPluginConfig { /** * Restart the WebView from native code when the renderer process dies. * * @default true */ restartOnCrash?: boolean;
/** * Fixed native interval, in milliseconds, for proactively replacing long-running WebViews. * * Set to `0` to disable interval restarts. Do not combine an active interval * with `restartCron`; native initialization fails fast when both schedules are configured. * * @default 0 */ restartIntervalMs?: number;
/** * Cron schedule for proactively replacing long-running WebViews. * * Uses standard 5-field cron syntax in the device local timezone: * `minute hour day-of-month month day-of-week`. * * Examples: * - `0 3 * * *` restarts every day at 03:00. * - `0,30 * * * *` restarts every 30 minutes. * * Do not combine this with an active `restartIntervalMs`; native initialization * fails fast when both schedules are configured. */ restartCron?: string;
/** * Delay, in milliseconds, before restarting after a crash. * * @default 0 */ restartAfterCrashDelayMs?: number;}WebViewCrashInfo
WebViewCrash信息export interface WebViewCrashInfo { /** * Platform that detected and stored the marker. */ platform: WebViewCrashPlatform;
/** * Unix timestamp in milliseconds for when the marker was written. */ timestamp: number;
/** * ISO-8601 version of `timestamp`. */ timestampISO: string;
/** * Platform-specific reason for the crash or restart marker. */ reason: WebViewCrashReason;
/** * Last known WebView URL when the marker was written. */ url?: string;
/** * Android-only hint from `RenderProcessGoneDetail.didCrash()`. */ didCrash?: boolean;
/** * Android-only renderer priority reported at exit. */ rendererPriorityAtExit?: number;
/** * iOS-only application state captured when the WebView process died. */ appState?: WebViewCrashAppState;}WebViewCrashPlatform
WebViewCrash平台export type WebViewCrashPlatform = 'android' | 'ios' | 'web';WebViewCrashReason
WebViewCrash原因export type WebViewCrashReason = | 'renderProcessGone' | 'webContentProcessDidTerminate' | 'periodicRestart' | 'manualRestart' | 'simulated';WebViewCrashAppState
WebViewCrash应用状态export type WebViewCrashAppState = 'active' | 'inactive' | 'background' | 'unknown';真实来源
名为“真实来源”的部分本页由插件的 src/definitions.ts重新同步公共API时,API发生变化。
继续从开始
名为“继续从开始”的部分如果您正在使用 开始 来规划原生媒体和界面行为,连接它到 使用@capgo/capacitor-webview-crash 为在使用@capgo/capacitor-webview-crash中原生能力 使用@capgo/capacitor-live-activities 为@capgo/capacitor-live-activities原生能力 @capgo/capacitor-live-activities 为@capgo/capacitor-live-activities的实现细节 使用@capgo/capacitor-video-player 为@capgo/capacitor-video-player原生能力 @capgo/capacitor-video-player 为@capgo/capacitor-video-player的实现细节