WKWebView 整体结构图
WKWebView
// 初始化
- (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration
// 加载网页端的 URL
- (nullable WKNavigation *)loadRequest:(NSURLRequest *)request;
// 以指定中心页面缩放内容
- (void)setMagnification:(CGFloat)magnification centeredAtPoint:(CGPoint)point;
WKWebViewConfiguration
WKWebView 配置类:进程池,WK偏好设置,native与站点存储
改配置只在初始化webview的时候生效,后面在修改配置,不会影响该webview的变化。
// 全部 load 进内存中再渲染,还是边load边渲染
@property (nonatomic) BOOL suppressesIncrementalRendering;
// 对用户代理设置
@property (nullable, nonatomic, copy) NSString *applicationNameForUserAgent
// 是否允许 AirPlay 播放媒体
@property (nonatomic) BOOL allowsAirPlayForMediaPlayback
// 给 URLScheme 设置一个通信协议,监听 URL Scheme 的加载状态
- (void)setURLSchemeHandler:(nullable id <WKURLSchemeHandler>)urlSchemeHandler forURLScheme:(NSString *)urlScheme
// 返回当前已注册的 URLScheme 对象
- (nullable id <WKURLSchemeHandler>)urlSchemeHandlerForURLScheme:(NSString *)urlScheme
WKProcessPool 进程池
解决多个webview共享登录态的问题
WKPreferences 偏好设置
// 最小的字体大小
@property (nonatomic) CGFloat minimumFontSize;
// 客户端点击前端是否可以交互
@property (nonatomic) BOOL javaScriptEnabled;
javaScript 是否能够自动弹出对话框,在没有用户交互的情况下,在iOS 默认是不可以的,在 OSX 上是可以的
@property (nonatomic) BOOL javaScriptCanOpenWindowsAutomatically;
浏览器是否支持java,默认 No
@property (nonatomic) BOOL javaEnabled;
// 是否支持插件,默认是 NO
@property (nonatomic) BOOL plugInsEnabled;
// 选项卡是链接
@property (nonatomic) BOOL tabFocusesLinks API_AVAILABLE(macosx(10.12.3));
总结:WKPreferences 是关于 JS 和 WK 一些交互方面的问题,比如,最小支持字体大小,能不能够支持 wk 和 JS 交互,JS 能不能主动弹出对话框,WK支不支持 JAVA,插件,以及选项卡是不是链接等。
WKUserContentController
// 与控制器相关的控制脚本
@property (nonatomic, readonly, copy) NSArray<WKUserScript *> *userScripts;
WKUserScript
// 脚本的源代码
@property (nonatomic, readonly, copy) NSString *source;
// 脚本应该被注入的时间
@property (nonatomic, readonly) WKUserScriptInjectionTime injectionTime;
// 表示脚本应该注入到主 frame中还是全部 frame中
@property (nonatomic, readonly, getter=isForMainFrameOnly) BOOL forMainFrameOnly;
// 添加一个用户脚本
- (void)addUserScript:(WKUserScript *)userScript;
// JS 调 OC,添加脚本处理
- (void)addScriptMessageHandler:(id <WKScriptMessageHandler>)scriptMessageHandler name:(NSString *)name;
// 移除一个脚本
- (void)removeScriptMessageHandlerForName:(NSString *)name;
WKWebsiteDataStore
代表网站可能提供的各种类型的数据,包括 cookies,磁盘,内存缓存,以及持久缓存。比如,WebSQL,IndexedDB和本地存储。
// 返回一个默认的存储方式
+ (WKWebsiteDataStore *)defaultDataStore;
// 非持久化数据存储,适用于"隐身模式"
+ (WKWebsiteDataStore *)nonPersistentDataStore;
// 数据是否持久化
@property (nonatomic, readonly, getter=isPersistent) BOOL persistent;
// 返回一组可用的网站数据类型的存储
+ (NSSet<NSString *> *)allWebsiteDataTypes;
// 包含给定网站的数据类型的数据记录
- (void)fetchDataRecordsOfTypes:(NSSet<NSString *> *)dataTypes completionHandler:(void (^)(NSArray<WKWebsiteDataRecord *> *))completionHandler;
为给定的数据记录删除给定的网站数据
- (void)removeDataOfTypes:(NSSet<NSString *> *)dataTypes forDataRecords:(NSArray<WKWebsiteDataRecord *> *)dataRecords completionHandler:(void (^)(void))completionHandler;
// 删除给定日期以来,修改的给定类型的所有网站数据
- (void)removeDataOfTypes:(NSSet<NSString *> *)websiteDataTypes modifiedSince:(NSDate *)date completionHandler:(void (^)(void))completionHandler;
WKHTTPCookieStore
// 获取所有存储的 cookie
- (void)getAllCookies:(void (^)(NSArray<NSHTTPCookie *> *))completionHandler;
// 设置 cookie
- (void)setCookie:(NSHTTPCookie *)cookie completionHandler:(nullable void (^)())completionHandler;
// 删除 cookie
- (void)deleteCookie:(NSHTTPCookie *)cookie completionHandler:(nullable void (^)())completionHandler;
// 添加一个注册协议来监听 cookie的变化
- (void)addObserver:(id<WKHTTPCookieStoreObserver>)observer;
// 删除 监听者
- (void)removeObserver:(id<WKHTTPCookieStoreObserver>)observer;
// 在请求发送之前,决定是否跳转 -> 如果方法不实现默认是跳转的。如果要实现该方法就要手动设置跳转。该方法执行在加载页面之前
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
例如:
// 需要自己实现跳转,该方法执行在加载页面之前
- (void)webView:(WKWebView *)webView
decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
decisionHandler(WKNavigationActionPolicyAllow);
NSLog(@"%s", __FUNCTION__);
}
// 在收到响应之后决定是否跳转,默认是跳转的,如果自己实现该方法需要实现跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler;
例子:
- (void)webView:(WKWebView *)webView
decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse
decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
{
decisionHandler(WKNavigationResponsePolicyAllow);
NSLog(@"%s", __FUNCTION__);
}
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation;
// 接收到服务器跳转请求是时调用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation;
// 页面加载失败的
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;
// 网页可以接收网页内容
- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation;
// 完成时加载
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation;
// 页面跳转失败
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;
// 当webview 需要证书验证时,调用
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler;
// webview中断时调用
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView;
WKUIDelegate
// 弹框关闭的时候,调用
- (void)webViewDidClose:(WKWebView *)webView;
// 弹出对话框
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler;
// 弹出确认按钮
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler;
// 弹出对话框
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable result))completionHandler;
// 创建新的webview时候调用的方法
- (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures;
WKBackForwardList
维护了用户访问过的网页记录,用来前进后退到加载的网页