** Solve the problem of webView loading URLs containing Chinese
Convert Chinese and other non-English characters in URL URLEncode
extension String { func urlEncodeIfNeed() -> String { if let url:URL = URL(string: self){ if(UIApplication.shared.canOpenURL(url) == true){ return self; } } return NSString.init(string: self).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)! } }
** Clear webView cache
let websetDataType = WKWebsiteDataStore.allWebsiteDataTypes(); let dateFrom:NSDate = NSDate.init(timeIntervalSince1970: 0) WKWebsiteDataStore.default().removeData(ofTypes: websetDataType, modifiedSince: dateFrom as Date, completionHandler: { print("clear done") })
//The type of WKWebsiteDataStory is let cookie = WKWebsiteDataTypeCookies;//cookies of the URL let disCache = WKWebsiteDataTypeDiskCache;//Disk cache files, such as image files let WebApplicationCache = WKWebsiteDataTypeOfflineWebApplicationCache;//Offline data let MemoryCache = WKWebsiteDataTypeMemoryCache;//Memory cache let LocalStorage = WKWebsiteDataTypeLocalStorage;//The website writes the local data through the code let SessionStorage = WKWebsiteDataTypeSessionStorage;//The website is written to the local session through code let IndexedDBDatabases = WKWebsiteDataTypeIndexedDBDatabases;// let WebSQLDatabases = WKWebsiteDataTypeWebSQLDatabases;//
If IOS 8 does not support the above method of clearing the cache, it is compatible with clearing, and manually delete the Library/Caches folder, etc.
http://mft.iteye.com/admin/blogs/2381013
You can also delete the folder under document directly as needed
http://mft.iteye.com/admin/blogs/2394856
** Solve the problem of opening a new window with target = "_blank"
import WebKit
Extend your class with WKUIDelegate
Set delegate for webview
self.webView.uiDelegate = self
Implement protocol method
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? { if navigationAction.targetFrame == nil { //webView.load(navigationAction.request) print(navigationAction.request); print("new target") } return nil }
** Get the URL of the previous page
wb.backForwardList.backItem?.url.absoluteString
** Determine whether the currently loaded page is by returning or forwarding or submitting a form, etc.
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { if(navigationAction.navigationType != .backForward){ }
** Remove the rubber band effect when pulling down
wkWebView.scrollView.bounces = false;
** load hml string
let jsUrl:String = "https://xw.qq.com"; var source:String = ""; let js_url = NSURL(string: jsUrl); do { print("1") let data = try NSString(contentsOf: js_url! as URL, encoding: UInt(String.Encoding.utf8.hashValue)) print("2") source = data as String; print(source); print("3") } catch { print("jjj false") } print("done") if(source != ""){ let wk:WKWebView = WKWebView(frame: self.view.frame); wk.loadHTMLString(source, baseURL: js_url! as URL) self.view.addSubview(wk) }
--------------------------------WKWebView 其他相关 知识 参考 ----------------------------------------------
swift -> WKWebView 滚动 监听 ,设置 滚动位置
swift -> WKWebview 长按 获取 html 标签