iOS WKWebView白屏(卡頓)

通過騰訊bugly卡頓日志發(fā)現(xiàn),有許多WebKit的卡頓信息,但是卻沒有定位到項目中的具體代碼。通過大量觀察發(fā)現(xiàn)很多日志中都有調(diào)用WebKit::WebPageProxy::processDidTerminate函數(shù)或WebKit::WebMemoryPressureHandler::WebMemoryPressureHandler()函數(shù)。參考WKWebView代理函數(shù)中的- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView;函數(shù)成因及含有*Memory*字段,因此大膽猜測可能是WKWebView內(nèi)存占用過大,內(nèi)存不足引起。

WKWebView 那些坑》文章中WKWebView白屏問題小節(jié)介紹到在WKWebView上當(dāng)總體的內(nèi)存占用比較大的時候,webContent process會crash,從而出現(xiàn)白屏現(xiàn)象。此時系統(tǒng)會調(diào)用- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView;函數(shù),在該函數(shù)里執(zhí)行[webView reload] (這個時候webView.URL取值尚不為nil)解決白屏問題。

兩者成因皆為WKWebView內(nèi)存占用過大引起,故本次卡頓解決方式參考WKWebView白屏問題解決方法。

@property (nonatomic ,assign) BOOL webViewLoadTitle;//是否已獲取當(dāng)前webView的title

#pragma mark -

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (YES == self.webViewLoadTitle && [NSString isNullDataString:self.webView.title]) {
        //webView已獲取title,但返回時無法獲取title,可能是內(nèi)存不足原因造成,此時需重新加載同時避免第一次進入時重新加載
        [self.webView reload];
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (![NSString isNullDataString:self.webView.title]) {
        self.webViewLoadTitle = YES;
    }
}

#pragma mark - WKNavigationDelegate

//web內(nèi)存過大,進程終止,重新加載webView
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {
    [webView reload];
}

結(jié)果:經(jīng)生產(chǎn)版本檢驗,可有效防止WKWebView內(nèi)存占用過大引起的卡頓問題。

卡頓日志參考

0 WebKit    WebKit::SandboxExtension::HandleArray::HandleArray()
1 WebKit    WebKit::WebProcessCreationParameters::WebProcessCreationParameters() + 36
2 WebKit    WebKit::WebProcessPool::initializeNewWebProcess(WebKit::WebProcessProxy&, WebKit::WebsiteDataStore&) + 156
3 WebKit    WebKit::WebProcessPool::createNewWebProcess(WebKit::WebsiteDataStore&, WebKit::WebProcessProxy::IsInPrewarmedPool) + 60
4 WebKit    WebKit::WebProcessPool::createNewWebProcessRespectingProcessCountLimit(WebKit::WebsiteDataStore&) + 232
5 WebKit    WebKit::WebPageProxy::reattachToWebProcess() + 36
6 WebKit    WebKit::WebPageProxy::reattachToWebProcessForReload() + 52
7 WebKit    WebKit::WebPageProxy::reload(WTF::OptionSet<WebCore::ReloadOption>) + 268
8 WebKit    WebKit::WebPageProxy::tryReloadAfterProcessTermination() + 308
9 WebKit    WebKit::WebPageProxy::processDidTerminate(WebKit::ProcessTerminationReason) + 644
10 WebKit   WebKit::WebProcessProxy::processDidTerminateOrFailedToLaunch() + 736
11 WebKit   WebKit::WebProcessProxy::didClose(IPC::Connection&) + 160
12 JavaScriptCore   WTF::RunLoop::performWork() + 276
13 JavaScriptCore   WTF::RunLoop::performWork(void*) + 36
14 CoreFoundation   ___CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
0 libobjc.A.dylib   class_createInstance + 36
1 libobjc.A.dylib   _objc_rootAlloc + 52
2 libobjc.A.dylib   _objc_rootAlloc + 52
3 UIKitCore +[UITextInteraction textInteractionsForSet:] + 36
4 UIKitCore -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) addGestureRecognizersToView:] + 388
5 UIKitCore -[UIWKTextInteractionAssistant addGestureRecognizersToView:] + 68
6 UIKitCore -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setGestureRecognizers] + 204
7 UIKitCore -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) initWithView:textInteractionSet:] + 124
8 UIKitCore -[UIWKTextInteractionAssistant initWithView:] + 44
9 WebKit    -[WKContentView(WKInteraction) setUpTextSelectionAssistant] + 92
10 WebKit   -[WKContentView(WKInteraction) setupInteraction] + 1356
11 WebKit   WebKit::PageClientImpl::didRelaunchProcess() + 36
12 WebKit   WebKit::WebPageProxy::reattachToWebProcess(WTF::Ref<WebKit::WebProcessProxy, WTF::DumbPtrTraits<WebKit::WebProcessProxy> >&&, API::Navigation*, WebKit::WebPageProxy::ReattachForBackForward) + 912
13 WebKit   WebKit::WebPageProxy::reattachToWebProcess() + 80
14 WebKit   WebKit::WebPageProxy::reattachToWebProcessForReload() + 52
15 WebKit   WebKit::WebPageProxy::reload(WTF::OptionSet<WebCore::ReloadOption>) + 268
16 WebKit   WebKit::WebPageProxy::tryReloadAfterProcessTermination() + 308
17 WebKit   WebKit::WebPageProxy::processDidTerminate(WebKit::ProcessTerminationReason) + 644
18 WebKit   WebKit::WebProcessProxy::processDidTerminateOrFailedToLaunch() + 736
19 WebKit   WebKit::WebProcessProxy::didClose(IPC::Connection&) + 160
20 JavaScriptCore   WTF::RunLoop::performWork() + 276
21 JavaScriptCore   WTF::RunLoop::performWork(void*) + 36
22 CoreFoundation   ___CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
0 WebKit    0x00000001c05f4000 + 4428800
1 WebKit    WebKit::ViewSnapshotStore::singleton() + 52
2 WebKit    _ZN3WTF8FunctionIFvNS_8CriticalENS_11SynchronousEEE15CallableWrapperIZN6WebKit28installMemoryPressureHandlerEvE3$_0E4callES1_S2_ + 24
3 JavaScriptCore    WTF::MemoryPressureHandler::releaseMemory(WTF::Critical, WTF::Synchronous) + 92
4 JavaScriptCore    ___ZN3WTF21MemoryPressureHandler7installEv_block_invoke_2 + 140
0 libsystem_kernel.dylib    kevent_id + 8
1 libdispatch.dylib __dispatch_kq_poll + 332
2 libdispatch.dylib __dispatch_event_loop_wait_for_ownership$VARIANT$mp + 412
3 libdispatch.dylib ___DISPATCH_WAIT_FOR_QUEUE__ + 296
4 libdispatch.dylib __dispatch_sync_f_slow + 140
5 AssertionServices -[BKSAssertion setInvalidationHandler:] + 144
6 WebKit    WebKit::ProcessAssertion::ProcessAssertion(int, WebKit::AssertionState, WTF::Function<void ()>&&) + 472
7 WebKit    WebKit::ProcessAndUIAssertion::ProcessAndUIAssertion(int, WebKit::AssertionState) + 96
8 WebKit    WebKit::ProcessThrottler::didConnectToProcess(int) + 224
9 WebKit    WebKit::WebProcessProxy::didFinishLaunching(WebKit::ProcessLauncher*, IPC::Connection::Identifier) + 576
10 WebKit   WebKit::ProcessLauncher::didFinishLaunchingProcess(int, IPC::Connection::Identifier) + 124
11 WebKit   ___ZN6WebKit15ProcessLauncher13launchProcessEv_block_invoke + 136
12 libxpc.dylib __xpc_connection_reply_callout + 60
0 WebKit    -[WKContentView(WKInteraction) isEditable]
1 UIKitCore -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) addGestureRecognizersToView:] + 120
2 UIKitCore -[UIWKTextInteractionAssistant addGestureRecognizersToView:] + 68
3 UIKitCore -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setGestureRecognizers] + 200
4 UIKitCore -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) initWithView:textInteractionSet:] + 124
5 UIKitCore -[UIWKTextInteractionAssistant initWithView:] + 44
6 WebKit    -[WKContentView(WKInteraction) setUpTextSelectionAssistant] + 92
7 WebKit    -[WKContentView(WKInteraction) setupInteraction] + 1636
8 WebKit    WebKit::PageClientImpl::didRelaunchProcess() + 36
9 WebKit    WebKit::WebPageProxy::finishAttachingToWebProcess(WebKit::WebPageProxy::ShouldInitializeWebPage) + 460
10 WebKit   WebKit::WebPageProxy::reattachToWebProcess() + 388
11 WebKit   WebKit::WebPageProxy::reattachToWebProcessForReload() + 348
12 WebKit   WebKit::WebPageProxy::reload(WTF::OptionSet<WebCore::ReloadOption>) + 436
13 WebKit   WebKit::WebPageProxy::tryReloadAfterProcessTermination() + 272
14 WebKit   WebKit::WebPageProxy::dispatchProcessDidTerminate(WebKit::ProcessTerminationReason) + 268
15 WebKit   WebKit::WebPageProxy::processDidTerminate(WebKit::ProcessTerminationReason) + 684
16 WebKit   WebKit::WebProcessProxy::processDidTerminateOrFailedToLaunch() + 916
17 WebKit   WebKit::WebProcessProxy::didClose(IPC::Connection&) + 140
18 JavaScriptCore   WTF::RunLoop::performWork() + 340
19 JavaScriptCore   WTF::RunLoop::performWork(void*) + 36
20 CoreFoundation   0x000000023a172000 + 700188
0 WebKit    API::Navigation::Navigation(WebKit::WebNavigationState&)
1 WebKit    WebKit::WebNavigationState::createReloadNavigation() + 44
2 WebKit    WebKit::WebPageProxy::reattachToWebProcessForReload() + 372
3 WebKit    WebKit::WebPageProxy::reload(WTF::OptionSet<WebCore::ReloadOption>) + 436
4 WebKit    WebKit::WebPageProxy::tryReloadAfterProcessTermination() + 272
5 WebKit    WebKit::WebPageProxy::dispatchProcessDidTerminate(WebKit::ProcessTerminationReason) + 268
6 WebKit    WebKit::WebPageProxy::processDidTerminate(WebKit::ProcessTerminationReason) + 684
7 WebKit    WebKit::WebProcessProxy::processDidTerminateOrFailedToLaunch() + 916
8 WebKit    WebKit::WebProcessProxy::didClose(IPC::Connection&) + 140
9 JavaScriptCore    WTF::RunLoop::performWork() + 276
10 JavaScriptCore   WTF::RunLoop::performWork(void*) + 36
0 WebKit    WTF::Mapper<WTF::Vector<WTF::String, 0ul, WTF::CrashOnOverflow, 16ul> WTF::copyToVectorOf<WTF::String, WTF::HashSet<WTF::String, WTF::StringHash, WTF::HashTraits<WTF::String> > >(WTF::HashSet<WTF::String, WTF::StringHash, WTF::HashTraits<WTF::String> > const&)::'lambda'(WTF::String const&), WTF::HashSet<WTF::String, WTF::StringHash, WTF::HashTraits<WTF::String> > const&, void>::map(WTF::HashSet<WTF::String, WTF::StringHash, WTF::HashTraits<WTF::String> > const&, WTF::Vector<WTF::String, 0ul, WTF::CrashOnOverflow, 16ul> WTF::copyToVectorOf<WTF::String, WTF::HashSet<WTF::String, WTF::StringHash, WTF::HashTraits<WTF::String> > >(WTF::HashSet<WTF::String, WTF::StringHash, WTF::HashTraits<WTF::String> > const&)::'lambda'(WTF::String const&) const&)
1 WebKit    WebKit::WebProcessPool::initializeNewWebProcess(WebKit::WebProcessProxy&, WebKit::WebsiteDataStore&) + 836
2 WebKit    WebKit::WebProcessPool::createNewWebProcess(WebKit::WebsiteDataStore&, WebKit::WebProcessProxy::IsPrewarmed) + 84
3 WebKit    WebKit::WebProcessPool::createNewWebProcessRespectingProcessCountLimit(WebKit::WebsiteDataStore&) + 228
4 WebKit    WebKit::WebPageProxy::reattachToWebProcess() + 252
5 WebKit    WebKit::WebPageProxy::reattachToWebProcessForReload() + 348
6 WebKit    WebKit::WebPageProxy::reload(WTF::OptionSet<WebCore::ReloadOption>) + 436
7 WebKit    WebKit::WebPageProxy::tryReloadAfterProcessTermination() + 272
8 WebKit    WebKit::WebPageProxy::dispatchProcessDidTerminate(WebKit::ProcessTerminationReason) + 268
9 WebKit    WebKit::WebPageProxy::processDidTerminate(WebKit::ProcessTerminationReason) + 684
10 WebKit   WebKit::WebProcessProxy::processDidTerminateOrFailedToLaunch() + 916
11 WebKit   WebKit::WebProcessProxy::didClose(IPC::Connection&) + 140
12 JavaScriptCore   WTF::RunLoop::performWork() + 340
13 JavaScriptCore   WTF::RunLoop::performWork(void*) + 36
0 WebKit    WebKit::ViewSnapshotStore::singleton()
1 WebKit    invocation function for block in WebKit::WebMemoryPressureHandler::WebMemoryPressureHandler() + 12
2 libdispatch.dylib __dispatch_client_callout + 16
0 WebKit    WebKit::SecItemShimProxy::singleton()
1 WebKit    WebKit::WebProcessProxy::connectionWillOpen(IPC::Connection&) + 48
2 WebKit    WebKit::ChildProcessProxy::didFinishLaunching(WebKit::ProcessLauncher*, IPC::Connection::Identifier) + 196
3 WebKit    WebKit::WebProcessProxy::didFinishLaunching(WebKit::ProcessLauncher*, IPC::Connection::Identifier) + 96
4 WebKit    WebKit::ProcessLauncher::didFinishLaunchingProcess(int, IPC::Connection::Identifier) + 124
5 WebKit    ___ZN6WebKit15ProcessLauncher13launchProcessEv_block_invoke + 136
6 libxpc.dylib  __xpc_connection_reply_callout + 60
0 WebCore   WebCore::HTTPHeaderMap::HTTPHeaderMap() + 0
1 WebKit    WebCore::ResourceRequestBase::ResourceRequestBase(WTF::URL const&, WebCore::ResourceRequestCachePolicy) + 152
2 WebKit    WebCore::ResourceRequest::ResourceRequest() + 48
3 WebKit    API::Navigation::Navigation(WebKit::WebNavigationState&) + 64
4 WebKit    WebKit::WebNavigationState::createReloadNavigation() + 44
5 WebKit    WebKit::WebPageProxy::reattachToWebProcessForReload() + 372
6 WebKit    WebKit::WebPageProxy::reload(WTF::OptionSet<WebCore::ReloadOption>) + 436
7 WebKit    WebKit::WebPageProxy::tryReloadAfterProcessTermination() + 272
8 WebKit    WebKit::WebPageProxy::dispatchProcessDidTerminate(WebKit::ProcessTerminationReason) + 268
9 WebKit    WebKit::WebPageProxy::processDidTerminate(WebKit::ProcessTerminationReason) + 684
10 WebKit   WebKit::WebProcessProxy::processDidTerminateOrFailedToLaunch() + 916
11 WebKit   WebKit::WebProcessProxy::didClose(IPC::Connection&) + 140
12 JavaScriptCore   WTF::RunLoop::performWork() + 340
13 JavaScriptCore   WTF::RunLoop::performWork(void*) + 36
14 CoreFoundation   0x00000001a728a000 + 700188
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,967評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,273評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,870評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,742評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 71,527評論 6 407
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,010評論 1 322
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,108評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,250評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,769評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,656評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,853評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,371評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,103評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,472評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,717評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,487評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 47,815評論 2 372

推薦閱讀更多精彩內(nèi)容

  • 原文 導(dǎo)語 WKWebView 是蘋果在 WWDC 2014 上推出的新一代 webView 組件,用以替代 UI...
    MR_詹閱讀 5,232評論 0 9
  • 1、WKWebView 白屏問題WKWebView 自詡擁有更快的加載速度,更低的內(nèi)存占用,但實際上 WKWebV...
    無名感恩閱讀 2,146評論 0 3
  • WKWebView 是蘋果在 WWDC 2014 上推出的新一代 webView 組件,用以替代 UIKit 中笨...
    Aiana閱讀 4,610評論 1 8
  • 轉(zhuǎn)載:http://www.cnblogs.com/NSong/p/6489802.html 導(dǎo)語 WKWebVi...
    李小威閱讀 4,871評論 8 9
  • 文|零度橘子 曾幾何時 我在河的這頭 你在河的那頭 中間隔著一條長長的河 夏天常遇暴雨,洪水圍困 冬天水流依舊,冰...
    零度橘子閱讀 745評論 5 34