这是仅有的几次认认真真写点东西,那里写的不明确欢迎指出;
注意, 注意 注意 进入正题了
WKWebView 是Apple 在iOS8 之后推出一个高效率、低内存的浏览网页的一个组件, 包含在WebKit 这个框架中, iOS 与 mac os 中的 Safari 都是基于WKWebView 实现的
一、 WKWebView 的特性:
- 在性能、稳定性、功能方面有很大提升(最直观的体现就是加载网页是占用的内存,模拟器加载百度与开源中国网站时,WKWebView占用23M,而UIWebView占用85M);
- 允许JavaScript的Nitro库加载并使用(UIWebView中限制);
- 支持了更多的HTML5特性;
- 高达60fps的滚动刷新率以及内置手势;
- 将UIWebViewDelegate与UIWebView重构成了14类与3个协议(查看苹果官方文档);
二、WKWebView属性介绍:
1、 只读的配置属性 WKWebViewConfiguration
2、 导航代理 navigationDelegate
3、 用户交互代理UIDelegate
4、 前进后退列表 backForwardList
5、 网页标题可以用kvo监听title
6、 请求的url 可用kvo监听 url
7、 当前是否正在加载网页可用kvo监听 loading
8、 加载进度范围0~1可用kvo监听 estimatedProgress
9、 标识页面中的所有资源是否通过安全加密连接来加载,可用kvo监听hasOnlySecureContent
10、当前导航的证书链 可用kvo监听 certificateChain
11、是否可以返回上一页可用kvo监听canGoBack
12、是否可以进入下一页可用kvo监听 canGoForward
13、是否支持左右的swipe手势是否可以前进、后退allowsBackForwardNavigationGestures
14、用户自定义的user agent 没有则为nil customUserAgent
15、在iOS上默认为NO,标识不允许链接预览allowsLinkPreview
16、展示网页内容的scrollview
17、是否可以放大默认为no allowsMagnification
18、放大因子默认1 magnification
三、WKWebView 方法:
1、 默认构造器
-(instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration*)configuration NS_DESIGNATED_INITIALIZER;
2、构造器<弃用>
-(instancetype)initWithCoder:(NSCoder *)coder NS_UNAVAILABLE;
3、通过request对象加载数据
-(nullable WKNavigation *)loadRequest:(NSURLRequest *)request;
4、通过url加载 iOS9+启用
-(nullable WKNavigation *)loadFileURL:(NSURL *)URLallowingReadAccessToURL:(NSURL *)readAccessURL NS_AVAILABLE(10_11, 9_0);
5、直接加载HTML
-(nullable WKNavigation *)loadHTMLString:(NSString *)string baseURL:(nullableNSURL *)baseURL;
6、直接加载data
-(nullable WKNavigation *)loadData:(NSData *)data MIMEType:(NSString *)MIMETypecharacterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL*)baseURL NS_AVAILABLE(10_11, 9_0);
7、前进或者后退到某页
-(nullable WKNavigation *)goToBackForwardListItem:(WKBackForwardListItem *)item;
8、返回上一页 如果不能返回则什么都不做
-(nullable WKNavigation *)goBack;
9、前进一页 如果不能前进则什么都不做
-(nullable WKNavigation *)goForward;
10、重新加载页面
-(nullable WKNavigation *)reload;
11、重新加载原始URL
-(nullable WKNavigation *)reloadFromOrigin;
12、停止加载
-(void)stopLoading;
13、执行JS代码
-(void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void(^ __nullable)(__nullable id, NSError * __nullable error))completionHandler;
14、根据设置的缩放因子来缩放页面,并居中显示结果在指定的点
-(void)setMagnification:(CGFloat)magnificationcenteredAtPoint:(CGPoint)point;
四、WKWebViewConfiguration 简介:
1、内容处理池
内容处理池 WKProcessPool并没有公开任何的属性或者方法,不需要配置:
WKProcessPool 继承自NSObject 无其他属性与方法
@property (nonatomic, strong) WKProcessPool *processPool;
2、WKPreferences WebView偏好配置
webView的偏好设置 preferences对象关联到一个webView是由其指定webView的配置
WKPreferences继承自 NSObject
WKPreferences:
属性
minimumFontSize 最小点的字体尺寸 默认0
javaScriptEnabled 指示是否启用JavaScript 默认YES
javaScriptCanOpenWindowsAutomatically 在iOS上默认为NO,JavaScript能否自动通过窗口打开
javaEnabled 是否启用Java 默认为NO
plugInsEnabled 是否启用插件 默认NO
@property (nonatomic, strong) WKPreferences *preferences
3、WKUserContentController 通过JS与webView交互
要通过JS与webview内容交互,就需要到这个类
WKUserContentController继承自NSObject
/WKUserContentController:
属性
userScripts 只读数组内不存储所有添加的WKUserScript对象
方法
addUserScript: 添加WKUserScript对象
removeAllUserScripts 移除所有WKUserScript对象
addScriptMessageHandler: 添加scriptMessageHandler到所有的frames中,如JS要调用我们原生的方法,就可以通过这种方式了
removeScriptMessageHandlerForName: 根据name移除所注入的scriptMessageHandler
@property (nonatomic, strong) WKUserContentController *userContentController;
4、WebView内容储存
iOS9.0以后才能使用这个类。它是代表webview不同的数据类型,包括cookies、disk、memory caches、WebSQL、IndexedDB数据库和本地存储
@property (nonatomic, strong) WKWebsiteDataStore *websiteDataStore
5、指示是否web视图内容呈现,直到它完全被加载到内存中 默认NO
@property (nonatomic) BOOL suppressesIncrementalRendering;
6、应用程序的名称中使用的用户代理字符串 iOS9+
@property (nullable, nonatomic, copy) NSString *applicationNameForUserAgent;
7、是否允许播放媒体 默认yes iOS9+
@property (nonatomic) BOOL allowsAirPlayForMediaPlayback;
7、 此属性有两个枚举值可选WKSelectionGranularityDynamic(默认) 和 WKSelectionGranularityCharacter
WKSelectionGranularityCharacter : 用户长按复制文字的选择区域是非用户自定义
WKSelectionGranularityDynamic: 用户长按复制文字的选择区域是用户自定义
@property (nonatomic) WKSelectionGranularity selectionGranularity;