WKWebView是在iOS8中发布的新的Web视图,旨在替换iOS中的UIWebView和macOS中的WebView。WKWebView很好的解决了UIWebView的内存占用大和加载速度慢的问题。
WKWebView可以加载本地HTML代码或者网络资源
//加载网络资源时我们一般采用的是异步的加载方式,则使用以下这个方法来加载:
//该方法需要的参数是NSURLRequest对象,必须要严格遵守某种协议
//日常上网的时候我们会把网址前的http://给省略,这个就是HTTP协议,在这里绝对不能省略
//由于采用异步请求加载网络资源,所以还要实现相应的WKNavigationDelegate委托协议。请求加载网络资源的不用阶段会触发委托对象不同的方法
- (nullable WKNavigation *)loadRequest:(NSURLRequest *)request;
//加载本地资源一般用同步方式,数据可以来源于本地文件或者是硬编码的的HTML字符串,相关方法如下:
//只用这两个方法时,我们需要注意字符集问题,而采用什么字符集取决于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));
下面这个案例展示了WKWebView的三种方法的用法,我通过三个按钮改变三种加载方式
#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委托协议
//开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation{
NSLog(@"开始加载");
}
//当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
NSLog(@"开始返回内容");
}
//加载完成后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
NSLog(@"加载完成");
}
//加载失败的时候调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error{
NSLog(@"加载失败 error:%@",error.description);
}
@end
在iOS9中Xcode引入了新的特性,在LoadRequest方法中请求网络资源时,必须使用HTTPS协议,但是很多情况下我们所访问的都是HTTP协议,所以需要去Info.plist文件中修改属性。如下图所示修改: