UIWebView是iOS 2 就有的 UIKit 中非常古老的加載網頁控件,WKWebView 是 iOS 8 之后才有的 WebKit 中的內容。蘋果推出WKWebView就是為了替換舊的UIWebView。
1.UIWebView 的優(yōu)缺點
- UIWebView會引起內存泄露,這是它系統(tǒng)本身的問題。
- 用UIWebView加載大數據資源時,內存峰值、內存都會暴增,同時CPU電量消耗也較高。
- UIWebView支持緩存 和 NSURLProtocol 攔截。
2.WKWebView的優(yōu)缺點
- 擁有高達60FPS滾動刷新率及內置手勢。
- 支持了更多的HTML5特性。
- 高效的app和web信息交換通道。
- 允許JavaScript的Nitro庫加載并使用,UIWebView中限制了。
- 提供加載網頁進度的屬性。
- WKWebView網頁加載速度有提升,更快(占用內存可能只有 UIWebView 的1/3~1/4)。
- 沒有緩存,更為細致地拆分了 UIWebViewDelegate 中的方法。//我建議如果對緩存不高的頁面可以使用,用戶體驗會提高很多。
- WKWebView 不支持緩存 和 NSURLProtocol 攔截.
3.UIWebView的三種load方法
- loadRequest
加載網絡資源
UIWebView*web = [[UIWebViewalloc]initWithFrame:self.view.bounds];
[self.viewaddSubview:web];
[webloadRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://www.baidu.com"]]];
//http請求需要在info文件里配置一下。App Transport Security Settings->Allow Arbitrary Loads = YES
除了加載網絡資源外,loadRequest 還可以加載本地資源。
//str2 == str3 != str1 str1多了file://
NSString*str1 = [[[NSBundlemainBundle]bundleURL]absoluteString];
NSString* str2 = [[NSBundlemainBundle]bundlePath] ;
NSString*str3 = [[NSBundlemainBundle]resourcePath] ;
NSString*path = [str3stringByAppendingPathComponent:@"test.html"];
[webloadRequest:[NSURLRequestrequestWithURL:[NSURLfileURLWithPath:path]]];
- loadHTMLString
這個方法用于直接加載html代碼。如果html沒有存在文件中,推薦使用這種方法。
UIWebView *web = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:web];
NSString *HTMLData = @"<hn>Hello World</hn>";
[web loadHTMLString:HTMLData baseURL:nil];
當然你也可以用這個方法從本地html讀取代碼,然后加載。但此時請注意baseURL要傳對,否則html中引用的資源是找不到的。
NSString*str3 = [[NSBundlemainBundle]resourcePath] ;
NSString*path = [str3stringByAppendingPathComponent:@"test.html"];
NSString*htmlstr = [[NSStringalloc]initWithContentsOfFile:pathencoding:NSUTF8StringEncodingerror:nil];
[webloadHTMLString:htmlstrbaseURL:[[NSBundlemainBundle]bundleURL]];
loadRequest和loadHTMLString都可以加載本地資源,蘋果給出了解釋:To help you avoid being vulnerable to security attacks, be sure to use this method to load local HTML files; don’t use loadRequest:。
意思是,為避免受到安全攻擊,一定要用這種方法來加載本地HTML文件;不要使用loadRequest:。
如果不從html文件載入你也可以這樣:
NSString *HTMLData = @"src=\"test2.png\"/>ddd";
[self.webView loadHTMLString:HTMLData baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]]];
baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]]
這段指出HTMLData所引用的其他文件資源的基本路徑,如果baseURL:nil圖片信息將不會顯示出來~
- loadData
使用loadData方法對文件進行加載,是以二進制方式加載數據,并且指定類型(mimetype)和編碼類型(textEncodingName)。
加載本地PDF
NSString *dataPath = [[NSBundle mainBundle]pathForResource:@"test" ofType:@"pdf"];
[_webView loadData:[NSData dataWithContentsOfFile:dataPath] MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:nil];
加載本地文本文件
NSString *dataPath = [[NSBundle mainBundle]pathForResource:@"誰胡傳" ofType:@"txt"];
[_webView loadData:[NSData dataWithContentsOfFile:dataPath] MIMEType:@"text/plain" textEncodingName:@"UTF-8" baseURL:nil];
4.WKWebView繼承了UIWebView中的這三種load方法,又增加了loadFileURL方法。(iOS9.0之后)
- loadRequest在WKWebView中只能加載網絡資源了,想要適配9.0之前的系統(tǒng)也可以通過一些間接的方式加載本地資源(不推薦)??梢杂胠oadHTMLString或loadData來加載本地資源。
- loadHTMLString和loadData幾乎沒有變化,加載時默認適配的終端不同。UIWebView默認是手機端WKWebView默認是pc端。
WKWebView中:


UIWebView中:

Snip20160902_1.png

Snip20160902_4.png
- loadFileURL
在iOS9的SDK中加入了該便利方法來加載本地的HTML文件。