iOS WKWebView后台崩溃问题排查
最近测试提出了一个bug,App在后台运行一段时间之后,在切换到前台时就自动跳转到了登录页,不是之前的页面了,经过两天的排查解决了问题,记录一下
- 我们的项目主要是网页,App只提供一个webview用来展示网页内容,所有的操作基本上都是在网页内完成的,可是测试说App在后台一段时间之后就会自动跳回网页的登录页,感觉应该是网页的问题。
- 首先想到是不是网页内的逻辑问题,找了前端网页的代码看看,发现普通请求报错403或者401的时候回直接跳转到登录页,感觉应该是这里导致的。问了测试是不是调用了什么接口报错导致跳回了登录页,回答是看后端日志没有接口报错,那这就比较奇怪了。因为这里可能性最大,所以我就一直测试,看是否能复现这个问题,试了试好像也没啥问题,不论抓包还是看log都没啥异常。
- 再三和测试确认了,发现只有最新款的iPhone13有这个问题,其他手机没问题。我想应该是iOS新系统导致的问题,没办法只能升级手机到最新系统iOS 15,在把手机连着电脑,同时开着Charles进行测试,果然登录几个小时之后,在XCode里看到了下面一段日志:
2021-12-07 17:33:37.675132+0800 xxx_app[1088:221122] [assertion] Error acquiring assertion: <Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist}> 2021-12-07 17:33:37.676720+0800 xxx_app[1088:221122] [ProcessSuspension] 0x1163faac0 - ProcessAssertion: Failed to acquire RBS assertion 'WebProcess Background Assertion' for process with PID=1095, error: Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist} 2021-12-07 17:33:37.678389+0800 xxx_app[1088:221122] [assertion] Error acquiring assertion: <Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist}> 2021-12-07 17:33:37.681365+0800 xxx_app[1088:197408] [Process] 0x116360190 - NetworkProcessProxy::didClose (Network Process 0 crash) 2021-12-07 17:33:37.684669+0800 xxx_app[1088:197408] [Process] 0x1163d0600 - [PID=1095] WebProcessProxy::didClose: (web process 0 crash) 2021-12-07 17:33:37.684717+0800 xxx_app[1088:197408] [Process] 0x1163d0600 - [PID=1095] WebProcessProxy::processDidTerminateOrFailedToLaunch: reason=4 2021-12-07 17:33:37.684823+0800 xxx_app[1088:197408] [ProcessSuspension] 0x1163faca0 - ProcessAssertion: Failed to acquire RBS Background assertion 'ConnectionTerminationWatchdog' for process because PID is invalid 2021-12-07 17:33:37.686948+0800 xxx_app[1088:197408] [Process] 0x1030b1e18 - [pageProxyID=5, webPageID=6, PID=1095] WebPageProxy::processDidTerminate: (pid 1095), reason 4 2021-12-07 17:33:37.678491+0800 xxx_app[1088:221122] [ProcessSuspension] 0x1163fab20 - ProcessAssertion: Failed to acquire RBS assertion 'NetworkProcess Background Assertion' for process with PID=1096, error: Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist} 2021-12-07 17:33:37.690991+0800 xxx_app[1088:221122] [assertion] Error acquiring assertion: <Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist}> 2021-12-07 17:33:37.691127+0800 xxx_app[1088:221122] [ProcessSuspension] 0x1163fab80 - ProcessAssertion: Failed to acquire RBS assertion 'WebProcess Foreground Assertion' for process with PID=1095, error: Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist} 2021-12-07 17:33:37.692469+0800 xxx_app[1088:221122] [assertion] Error acquiring assertion: <Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist}> 2021-12-07 17:33:37.692563+0800 xxx_app[1088:221122] [ProcessSuspension] 0x1163fabe0 - ProcessAssertion: Failed to acquire RBS assertion 'NetworkProcess Foreground Assertion' for process with PID=1096, error: Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist}
- 经过搜索和分析日志,发现应该是App在后台的时候,WebProcess被kill;当App切换前台时,会重新启动一个WebProcess,但是之前存在webview的session里的信息都没有了,导致网页直接跳转回来登录页。
- 找到问题就想有什么好的解决方法,比较简单就是在后台检测到webview被kill时,直接退出App,等下次切换到前台的时候相当于重新打开App;更好的方法是检测到webview被kill时,直接重新启动一个新的webview,把所有保存的信息复制过去;但是因为时间比较紧急就按照简单的方法解决了问题。
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView { NSLog(@"webViewWebContentProcessDidTerminate"); exit(0); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义