[iOS学习]WKWebView类的学习

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文件中修改属性。如下图所示修改:

Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,541评论 4 61
  • WkWebView是IOS8中引入的新组件,苹果将UIWebViewDelegate 与 UIWebView 重构...
    i_belive阅读 10,475评论 1 25
  • 很多人都喜欢着不敢上前,却甘心望着ta背影,说是因为喜欢或爱。。。 如果真的喜欢,真的爱,会甘愿只望着背影吗? 勇...
    貓丝儿阅读 4,256评论 0 3
  • 芽孢杆菌属微生物特性,芽孢杆菌属,革兰氏阳性菌,需氧或兼性厌氧菌产生芽孢,大多数无夹馍。 利...
    杰克高阅读 3,980评论 0 0
  • 本故事纯属虚构,如有雷同,实属倒霉╮(╯╰)╭ 宋仁少时,性平而善,曾随父母步行翻山至亲家。 事毕,急返家,时值冬...
    拉菲小优阅读 3,211评论 12 8

友情链接更多精彩内容