UIWebView就不用說了,這個過期了,如今iOS8之後建議都使用WKWebView。html
WKWebView
是現代 WebKit API 在 iOS 8 和 OS X Yosemite 應用中的核心部分。它代替了 UIKit 中的UIWebView
和 AppKit 中的 WebView
,提供了統一的跨雙平臺 API。ios
自誇擁有 60fps 滾動刷新率、內置手勢、高效的 app 和 web 信息交換通道、和 Safari 相同的 JavaScript 引擎,WKWebView
毫無疑問地成爲了 WWDC 2014 上的最亮點。web
【UIWebView 和 WKWebView 的區別】
WKWebView 更快(佔用內存可能只有 UIWebView 的1/3~1/4),沒有緩存,更爲細緻地拆分了 UIWebViewDelegate 中的方法。swift
【爲何如今是時候從 UIWebView 遷移到 WKWebView 了】
截止到我寫這篇文章的時候,據 mixpanel 的數據,iOS 9 佔有率已達 58.55%,iOS 8 佔有率達到了 34.78%,iOS 7 及更早版本是 6.66%,而那 6.66% 應該大部分都是對手機使用極度不頻繁的人。因此從如今開始,再開發 App 只兼容 iOS 8 和 iOS 9 兩個版本就能夠了(若是你的產品對覆蓋率要求不是很苛刻的話)。WKWebView 是 iOS 8 以後纔有的 WebKit 中的內容,因此以前咱們要同時兼容 iOS 7 和 iOS 8 的時候,能夠辭讓說 UIWebView 和 WKWebView 一塊兒作太麻煩了,如今可沒有理由拒絕新東西了。緩存
目前iOS各個版本市場使用率參見:https://developer.apple.com/support/app-store/微信
在 WKWebView 中,UIWebViewDelegate 與 UIWebView 被重構成了14類與3個協議,下面給出一些在 UIWebView 中經常使用的方法的 WKWebView 版本。app
//準備加載頁面 UIWebViewDelegate - webView:shouldStartLoadWithRequest:navigationType WKNavigationDelegate - webView:didStartProvisionalNavigation: //已開始加載頁面,能夠在這一步向view中添加一個過渡動畫 UIWebViewDelegate - webViewDidStartLoad: WKNavigationDelegate - webView:didCommitNavigation: //頁面已所有加載,能夠在這一步把過渡動畫去掉 UIWebViewDelegate - webViewDidFinishLoad: WKNavigationDelegate - webView:didFinishNavigation: //加載頁面失敗 UIWebViewDelegate - webView:didFailLoadWithError: WKNavigationDelegate - webView:didFailNavigation:withError: WKNavigationDelegate - webView:didFailProvisionalNavigation:withError:
以上方法分別存在於 UIWebViewDelegate 和 WKNavigationDelegate 中。
若是你以前只是用到了以上列出的 UIWebViewDelegate 中的幾個方法,那麼只是簡單地換一個方法名,讓你的 ViewController 繼承 WKNavigationDelegate ,繼續用就能夠了。想要更多內容能夠本身用 cmd鍵+鼠標左擊『WKNavigationDelegate』經過 Xcode 查看。
要注意的是 webview.delegate = self
須要改寫爲 webview.navigationDelegate = self
。post
在 UIWebView 中,一句簡單的webView.stringByEvaluatingJavaScriptFromString()
就能夠用 JS 腳本操縱 WebView,在 WKWebView 中,咱們可能須要用到 WKScriptMessageHandler
這個協議中的 func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage)
方法。動畫
下面的示例代碼用於從 WKWebView 中獲取網頁中的文本。lua
let js = "window.webkit.messageHandlers.observe.postMessage(document.body.innerText);" // 注意這裏的observe字段是本身寫的,不是固定的寫法,參考第6行 let script = WKUserScript(source: js, injectionTime: WKUserScriptInjectionTime.AtDocumentEnd, forMainFrameOnly: true) // 這裏的 AtDocumentEnd 字段是指網頁中的內容加載完畢後再插入 JS 腳本,你也能夠選擇 AtDocumentStart,在 document element 剛剛建立時就插入腳本,看具體需求 let config = WKWebViewConfiguration() config.userContentController.addUserScript(script) config.userContentController.addScriptMessageHandler(self, name: "observe") // 對應第一行 JS 腳本中的observe字段 //初始化WKWebView let webview = WKWebView( frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height), configuration: config) webview.navigationDelegate = self self.view.addSubview(webview) //加載網頁 webview.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.jianshu.com")!))
可能你也注意到了,把 JS 腳本注入到 WebView 的途徑是初始化一個 WebView,因此你須要在 WebView 初始化以前寫好本身的腳本。固然若是你不須要 JS 交互,直接用一個 frame 來初始化 WebView,去掉 configuration 參數就行了。
然而,咱們如何拿到從 WKWebView 中抓取到的文本呢(經過 document.body.innerText 這一句)?
如上面所說,讓你的 ViewController 在繼承了 WKNavigationDelegate 以後再繼承一下 WKScriptMessageHandler 。而後實現 WKScriptMessageHandler 中惟一的一個方法:
func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage){ let str:NSString = message.body as! NSString // 由於咱們抓取到的是文本,這裏把 message.body 強制轉換爲 NSString,若是你經過 JS 拿到的是其餘信息,按需轉換 print(str) }
乍一看這裏可能會有疑惑:咱們給 WKWebView 綁定了 WKNavigationDelegate,可是 WKScriptMessageHandler 並無提供代理方法啊。其實與此相對應的過程出如今上一段代碼中的第5行:config.userContentController.addScriptMessageHandler
這一句。
iOS開發, 混編成爲一種趨勢。在中國,用戶量較大的幾個表明性app,淘寶,微信,支付寶,京東等,無不採用H5與 native混編模式。H5與native的交互變得愈來愈頻繁,從而出現的問題也愈來愈多,下面咱們一塊兒討論關於WKWebView的緩存問題。
場景:
當咱們的APP已經開發完成,上傳appstore之後,H5更新將再也不受appstore得限制,能夠隨時去作更改。但WKWebView 有默認的緩存功能,即使H5作了更改,app上顯示的依舊是老得頁面。爲了解決這一問題,咱們就會用到WKWebView的刪除 緩存功能。
iOS9 WKWebView新方法:
NSSet *websiteDataTypes = [NSSet setWithArray:@[ WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeOfflineWebApplicationCache, WKWebsiteDataTypeMemoryCache, WKWebsiteDataTypeLocalStorage, WKWebsiteDataTypeCookies, WKWebsiteDataTypeSessionStorage, WKWebsiteDataTypeIndexedDBDatabases, WKWebsiteDataTypeWebSQLDatabases ]]; //你能夠選擇性的刪除一些你須要刪除的文件 or 也能夠直接所有刪除全部緩存的type //NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes]; NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0]; [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{ // code }];
但開發app必需要兼容全部iOS版本,但是iOS8,iOS7沒有這種直接的方法,那該怎麼辦呢? (iOS7.0只有UIWebView, 而iOS8.0是有WKWebView, 但8.0的WKWebView沒有刪除緩存方法。) 針對與iOS7.0、iOS8.0、iOS9.0 WebView的緩存,咱們找到了一個通吃的辦法:
NSString *libraryDir = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0]; NSString *bundleId = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"]; NSString *webkitFolderInLib = [NSString stringWithFormat:@"%@/WebKit",libraryDir]; NSString *webKitFolderInCaches = [NSString stringWithFormat:@"%@/Caches/%@/WebKit",libraryDir,bundleId]; NSString *webKitFolderInCachesfs = [NSString stringWithFormat:@"%@/Caches/%@/fsCachedData",libraryDir,bundleId]; NSError *error; /* iOS8.0 WebView Cache的存放路徑 */ [[NSFileManager defaultManager] removeItemAtPath:webKitFolderInCaches error:&error]; [[NSFileManager defaultManager] removeItemAtPath:webkitFolderInLib error:nil]; /* iOS7.0 WebView Cache的存放路徑 */ [[NSFileManager defaultManager] removeItemAtPath:webKitFolderInCachesfs error:&error];
H5更新發版的的時候,作一次清除WebView緩存, app的WebView就會顯示最新的H5頁面了。