WKWebView調(diào)用goBack返回時,頁面不會刷新畔况,那么對于頁面需要登陸的情況就很容易出現(xiàn)BUG鲸鹦,從別的頁面登陸了,返回時跷跪,當(dāng)前頁面還需要登陸馋嗜,體驗很是不好。
下面是我綜合網(wǎng)上的思路吵瞻,收集的兩個解決方案葛菇,僅供參考。
方案一
在返回的同時橡羞,手動調(diào)用重新裝填方法眯停,如下:
[_webView goBack];
[_webView reload];
此方案有個缺陷:那就是前進(jìn)和后退導(dǎo)航會出問題;
方案二(推薦)
通過注入一段JS卿泽,在頁面返回的時候莺债,觸發(fā)JS重新加載頁面鏈接,大致如下:
WKWebViewConfiguration *config = [WKWebViewConfiguration new];
WKUserContentController *userContentController = [WKUserContentController new];
WKUserScript *reloadScript = [[WKUserScript alloc] initWithSource:@"window.addEventListener('pageshow', function(event){if(event.persisted){location.reload();}});"
injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
forMainFrameOnly:YES];
[userContentController addUserScript: reloadScript];
config.userContentController = userContentController;
這樣當(dāng)WKWebView后退的時候签夭,就會觸發(fā)JS的pageshow
方法齐邦,進(jìn)而觸發(fā)頁面刷新。
總結(jié)
經(jīng)測試覆致,方案更完美一些侄旬。