参考文章:http://www.cocoachina.com/ios/20180831/24753.html
WK时苹果在iOS8.0之后推出的控件,相比于UIWebView:
- 内存消耗少;
- 解决了网页加载时的内存泄漏问题;
- 与HTML页面的交互更方便;
- 总之,其性能比UIWebView好很多。
使用时,首先要添加头文件:
#import <WebKit/WebKit.h>
简单创建一个WKWebView:
self.iWKWebView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64)]; //此处协议下面会讲到 self.iWKWebView.navigationDelegate = self; self.iWKWebView.UIDelegate = self; self.iWKWebView.allowsBackForwardNavigationGestures = YES; NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [self.iWKWebView loadRequest:request]; [self.view addSubview:self.iWKWebView];
基本用法和UIWebView差不多。
这里介绍几个主要的类:
- WKWebView
-
WKWebViewConfiguration
-
WKPreferences
-
WKUserContentController
-
WKWebsiteDataStore
1. WKWebView:
常用属性:
// 导航代理 @property (nullable, nonatomic, weak) id <WKNavigationDelegate> navigationDelegate; // UI代理 @property (nullable, nonatomic, weak) id <WKUIDelegate> UIDelegate; // 页面标题, 一般使用KVO动态获取 @property (nullable, nonatomic, readonly, copy) NSString *title; // 页面加载进度, 一般使用KVO动态获取 @property (nonatomic, readonly) double estimatedProgress; // 可返回的页面列表, 已打开过的网页, 有点类似于navigationController的viewControllers属性 @property (nonatomic, readonly, strong) WKBackForwardList *backForwardList; // 页面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; // WKWebView继承自UIView, 所以如果想设置scrollView的一些属性, 需要对此属性进行配置 @property (nonatomic, readonly, strong) UIScrollView *scrollView; // 是否允许手势左滑返回上一级, 类似导航控制的左滑返回 @property (nonatomic) BOOL allowsBackForwardNavigationGestures; //自定义UserAgent, 会覆盖默认的值 ,iOS 9之后有效 @property (nullable, nonatomic, copy) NSString *customUserAgent
常用方法:
// 带配置信息的初始化方法 // configuration 配置信息 - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration // 加载请求 - (nullable WKNavigation *)loadRequest:(NSURLRequest *)request; // 加载HTML - (nullable WKNavigation *)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL; // 返回上一级 - (nullable WKNavigation *)goBack; // 前进下一级, 需要曾经打开过, 才能前进 - (nullable WKNavigation *)goForward; // 刷新页面 - (nullable WKNavigation *)reload; // 根据缓存有效期来刷新页面 - (nullable WKNavigation *)reloadFromOrigin; // 停止加载页面 - (void)stopLoading; // 执行java script代码 - (void)eva luatejava script:(NSString *)java scriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler;
2. WKWebViewConfiguration:配置信息
可以用配置信息来初始化WKWebView.
属性有:
//关于网页的设置 @property (nonatomic, strong) WKPreferences *preferences; //java script与原生交互的桥梁 @property (nonatomic, strong) WKUserContentController *userContentController; //提供了网站所能使用的数据类型 @property (nonatomic, strong) WKWebsiteDataStore *websiteDataStore API_AVAILABLE(macosx(10.11), ios(9.0)); //是否允许播放媒体文件 @property (nonatomic) BOOL allowsAirPlayForMedia