简介
通用型很强,程序员只需要能识别网页端,相应的就能开发对应的ios和androw版本,只需要把网页加载到一个控件里面,APP只提供了一个壳,具体的内容都是通过网页加载的,现在越来越流行了。
自iOS8开始,苹果推出了新框架WebKit,提供了替换UIWebView的组件WKWebView,一个用来展示网页的组件。WKWebView解决了UIWebView加载速度慢,占用内存多,优化困难等问题,可以说,WKWebView是目前App内部加载网页的最佳选择。
WKWebView是Webkit框架中提供的类。WKWebView与之前的UIWebView相比做了很多的功能提升,主要体现在如下几个方面。
在性能、稳定性、功能方面有很大提升,减少了加载网页时占用的内存
更好的支持HTML5的特性
高达60fps的滚动刷新率以及内置手势
允许JavaScript的Nitro库加载并使用
将UIWebViewDelegate与UIWebView重构成了14类与3个协议
属性:
WKWebView实际上是装载在一个scrollView里面来实现滑动效果的。
网页标题
@property (nullable, nonatomic, readonly, copy) NSString *title;
网页的url
@property (nullable, nonatomic, readonly, copy) NSURL *URL;
网页是否正在加载中
@property (nonatomic, readonly, getter=isLoading) BOOL loading;
是否允许回退
@property (nonatomic, readonly) BOOL canGoBack;
是否允许前进
@property (nonatomic, readonly) BOOL canGoForward;
标识是否支持左、右滑动手势来进行网页后退、前进
@property (nonatomic) BOOL allowsBackForwardNavigationGestures;
UI代理属性,用于响应用户交互
@property (nullable, nonatomic, weak) id UIDelegate;
与WKWebView相关联的scrollView
@property (nonatomic, readonly, strong) UIScrollView *scrollView;
WKWebView常用方法介绍
WKWebView类主要用于展示网页,因此在WKWebView类中提供了用于网页展示以及控制的方法,同时在WKWebView中也支持执行JavaScript代码。
WKWebView初始化方法
- (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration NS_DESIGNATED_INITIALIZER;
通过url加载网页视图 - (nullable WKNavigation *)loadRequest:(NSURLRequest *)request;
网页后退 - (nullable WKNavigation *)goBack;
网页前进 - (nullable WKNavigation *)goForward;
网页刷新 - (nullable WKNavigation *)reload;
停止加载 - (void)stopLoading;
执行JavaScript代码 - (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler;
基本使用-加载网页
准备工作
由于IOS9之后苹果默认不支持HTTP协议,所以我们需要对配置文件进行一些设置,使得我们能访问HTTP的地址。在项目的Info.plist文件中,找到APP Transport Security Settings,如果没有需要创建(+),添加Allow Arbitary loads 键值,类型为Boolean,并将它的值设为YES
//
// ViewController.m
// WKWebView_base
//
// Created by on 2019/7/13.
// Copyright © 2019 Shae. All rights reserved.
//
#import "ViewController.h"
#import <WebKit/WebKit.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//实例化WKWebView对象
WKWebView *webView=[[WKWebView alloc]initWithFrame:self.view.bounds];
//创建请求,NSURLRequest也可以
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://blog.csdn.net/qq_27597629"]];
//加载网页
[webView loadRequest:request];
[self.view addSubview:webView];
}
@end
当然目前没有做前进后退的操作,不能返回。
网页控制
刷新,前进,后退
网页后退
nullable WKNavigation *)goBack;
网页前进
(nullable WKNavigation *)goForward;
是否允许回退
@property (nonatomic, readonly) BOOL canGoBack;
是否允许前进
@property (nonatomic, readonly) BOOL canGoForward;
//
// ViewController.m
// WKWebView_base
//
// Created by on 2019/7/13.
// Copyright © 2019 Shae. All rights reserved.
//
#import "ViewController.h"
#import <WebKit/WebKit.h>
@interface ViewController ()
@property (nonatomic,strong) WKWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor darkGrayColor];
//实例化WKWebView对象
self.webView=[[WKWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width,self.view.bounds.size.height-50)];
//创建请求,NSURLRequest也可以
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://blog.csdn.net/qq_27597629"]];
//加载网页
[_webView loadRequest:request];
[self.view addSubview:_webView];
}
- (IBAction)goBack:(UIButton *)sender {
[self.webView goBack];
}
- (IBAction)reload:(UIButton *)sender {
[self.webView reload];
}
- (IBAction)goForward:(UIButton *)sender {
[self.webView goForward];
}
@end
加载本地文件
WKWebView除了能够加载网页之外,还能够加载多种类型的文件,支持的格式涵盖了pdf、word、txt、图片等。加载文件需要使用到loadFileURL:allowingReadAccessToURL:方法,。
- (IBAction)localFile:(UIButton *)sender {
self.localWebView=[[WKWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width,self.view.bounds.size.height-50)];
NSURL *url=[NSURL fileURLWithPath:@"/Users/xiexin/Downloads/0c5f0f3bca865b44476199f0325b8f1f.jpg"];
[self.localWebView loadFileURL:url allowingReadAccessToURL:url];
[self.view addSubview:_localWebView];
}
WKNavigationDelegate
WKNavigationDelegate是WKWebView最常用到的代理协议,其中提供的方法主要用来于捕获加载过程,WKNavigationDelegate中有如下几个常用的方法可以重点掌握。
页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation;
开始获取到网页内容时调用 - (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation;
页面加载完成之后调用 - (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation;