WKWebView調(diào)用goBack返回時,頁面不會刷新,那么對于頁面需要登陸的情況就很容易出現(xiàn)BUG,從別的頁面登陸了,返回時,當前頁面還需要登陸,體驗很是不好。
下面是我綜合網(wǎng)上的思路,收集的兩個解決方案,僅供參考。
方案一
在返回的同時,手動調(diào)用重新裝填方法,如下:
[_webView goBack];
[_webView reload];
此方案有個缺陷:那就是前進和后退導(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;
這樣當WKWebView后退的時候,就會觸發(fā)JS的pageshow方法,進而觸發(fā)頁面刷新。
總結(jié)
經(jīng)測試,方案更完美一些。