WKWebView是在iOS8中發(fā)布的新的Web視圖,旨在替換iOS中的UIWebView和macOS中的WebView。WKWebView很好的解決了UIWebView的內(nèi)存占用大和加載速度慢的問(wèn)題善绎。
WKWebView可以加載本地HTML代碼或者網(wǎng)絡(luò)資源
//加載網(wǎng)絡(luò)資源時(shí)我們一般采用的是異步的加載方式敛劝,則使用以下這個(gè)方法來(lái)加載:
//該方法需要的參數(shù)是NSURLRequest對(duì)象浑度,必須要嚴(yán)格遵守某種協(xié)議
//日常上網(wǎng)的時(shí)候我們會(huì)把網(wǎng)址前的http://給省略踩官,這個(gè)就是HTTP協(xié)議吆豹,在這里絕對(duì)不能省略
//由于采用異步請(qǐng)求加載網(wǎng)絡(luò)資源哎媚,所以還要實(shí)現(xiàn)相應(yīng)的WKNavigationDelegate委托協(xié)議租漂。請(qǐng)求加載網(wǎng)絡(luò)資源的不用階段會(huì)觸發(fā)委托對(duì)象不同的方法
- (nullable WKNavigation *)loadRequest:(NSURLRequest *)request;
//加載本地資源一般用同步方式阶女,數(shù)據(jù)可以來(lái)源于本地文件或者是硬編碼的的HTML字符串,相關(guān)方法如下:
//只用這兩個(gè)方法時(shí)哩治,我們需要注意字符集問(wèn)題张肾,而采用什么字符集取決于HTML文件
- (nullable WKNavigation *)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;
- (nullable WKNavigation *)loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL API_AVAILABLE(macosx(10.11), ios(9.0));
下面這個(gè)案例展示了WKWebView的三種方法的用法,我通過(guò)三個(gè)按鈕改變?nèi)N加載方式
#import "ViewController.h"
#import <WebKit/WebKit.h>
@interface ViewController ()<WKNavigationDelegate>
@property(nonatomic,strong)WKWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect screen = [UIScreen mainScreen].bounds;
///按鈕欄
//按鈕欄寬度
CGFloat buttonBarWidth = 316;
UIView *buttonBar = [[UIView alloc]initWithFrame:CGRectMake((screen.size.width - buttonBarWidth)/2, 20, buttonBarWidth, 30)];
[self.view addSubview:buttonBar];
///添加LoadHTMLString按鈕
UIButton *buttonLoadHTMLString = [UIButton buttonWithType:UIButtonTypeSystem];
[buttonLoadHTMLString setTitle:@"LoadHTMLString" forState:UIControlStateNormal];
buttonLoadHTMLString.frame = CGRectMake(0, 0, 117, 30);
[buttonLoadHTMLString addTarget:self action:@selector(testLoadHTMLString:) forControlEvents:UIControlEventTouchUpInside];
[buttonBar addSubview:buttonLoadHTMLString];
///添加LoadData按鈕
UIButton *buttonLoadData = [UIButton buttonWithType:UIButtonTypeSystem];
[buttonLoadData setTitle:@"LoadData" forState:UIControlStateNormal];
buttonLoadData.frame = CGRectMake(137, 0, 67, 30);
[buttonLoadData addTarget:self action:@selector(testLoadData:) forControlEvents:UIControlEventTouchUpInside];
[buttonBar addSubview:buttonLoadData];
///添加LoadRequest按鈕
UIButton *buttonLoadRequest = [UIButton buttonWithType:UIButtonTypeSystem];
[buttonLoadRequest setTitle:@"LoadRequest" forState:UIControlStateNormal];
buttonLoadRequest.frame = CGRectMake(224, 0, 92, 30);
[buttonLoadRequest addTarget:self action:@selector(testLoadRequest:) forControlEvents:UIControlEventTouchUpInside];
[buttonBar addSubview:buttonLoadRequest];
//添加WKWebView
self.webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 60, screen.size.width, screen.size.height - 80)];
[self.view addSubview:self.webView];
}
- (void)testLoadHTMLString:(id)sender{
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index.html" ofType:nil];
NSURL *bundleURl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSError *error = nil;
NSString *html = [[NSString alloc]initWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:&error];
if (error == nil) {
[self.webView loadHTMLString:html baseURL:bundleURl];
}
}
- (void)testLoadData:(id)sender{
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index.html" ofType:nil];
NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]];
NSData *htmlData = [[NSData alloc]initWithContentsOfFile:htmlPath];
[self.webView loadData:htmlData MIMEType:@"text/html" characterEncodingName:@"UTF-8" baseURL:bundleUrl];
}
- (void)testLoadRequest:(id)sender{
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
self.webView.navigationDelegate = self;
}
#pragma mark - WKNavigationDelegate委托協(xié)議
//開(kāi)始加載時(shí)調(diào)用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation{
NSLog(@"開(kāi)始加載");
}
//當(dāng)內(nèi)容開(kāi)始返回時(shí)調(diào)用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
NSLog(@"開(kāi)始返回內(nèi)容");
}
//加載完成后調(diào)用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
NSLog(@"加載完成");
}
//加載失敗的時(shí)候調(diào)用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error{
NSLog(@"加載失敗 error:%@",error.description);
}
@end
在iOS9中Xcode引入了新的特性锚扎,在LoadRequest方法中請(qǐng)求網(wǎng)絡(luò)資源時(shí),必須使用HTTPS協(xié)議馁启,但是很多情況下我們所訪問(wèn)的都是HTTP協(xié)議驾孔,所以需要去Info.plist文件中修改屬性。如下圖所示修改:
Paste_Image.png