美文网首页
WKWebView的使用

WKWebView的使用

作者: Carson_Zhu | 来源:发表于2018-02-14 19:07 被阅读31次

    简介

    iOS 8以后,苹果推出了新框架Webkit,提供了替换UIWebView的组件WKWebView。各种UIWebView的问题没有了,速度更快了,占用内存少了。

    特性

    • 在性能、稳定性、功能方面有很大提升(最直观的体现就是加载网页是占用的内存,模拟器加载百度与开源中国网站时,WKWebView占用23M,而UIWebView占用85M)。
    • 拥有高达60fps滚动刷新率及内置手势。
    • 支持了更多的HTML5特性。
    • 允许JavaScriptNitro库加载并使用(UIWebView中限制)。
    • UIWebViewDelegateUIWebView重构成了14类与3个协议查看苹果官方文档

    基本使用

    导入头文件
    #import <WebKit/WebKit.h>
    
    初始化
    // 初始化1
    - (instancetype)initWithFrame:(CGRect)frame;
    
    // 初始化2,WKWebViewConfiguration可以用来设置与js交互的相关配置
    - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration
    
    // 直接加载HTML
    - (nullable WKNavigation *)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;
    
    // 直接加载data
    - (nullable WKNavigation *)loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL NS_AVAILABLE(10_11, 9_0);
    
    加载页面
    // 根据NSURLRequest加载
    - (nullable WKNavigation *)loadRequest:(NSURLRequest *)request;
    // 直接根据URL加载 iOS 9.0以后方法
    - (nullable WKNavigation *)loadFileURL:(NSURL *)URL allowingReadAccessToURL:(NSURL *)readAccessURL API_AVAILABLE(macosx(10.11), ios(9.0));
    
    配置

    WKWebViewConfiguration是一个属性的集合,用来初始化WKWebView。这个类包含众多的属性,这里介绍几个常用的属性。

    • WKProcessPool
    // 内容处理池
    // 这个类没有公开的方法和属性,而且也并不需要配置,可以暂时忽略。
    
    • WKPreferences
    // 初始化一个WKWebViewConfiguration对象
    WKWebViewConfiguration *config = [WKWebViewConfiguration new];
    // 初始化偏好设置属性:preferences
    config.preferences = [WKPreferences new];
    // 默认为0
    config.preferences.minimumFontSize = 10;
    // 是否支持JavaScript,默认认为YES
    config.preferences.javaScriptEnabled = YES;
    // 不通过用户交互,是否可以打开窗口,默认为NO
    config.preferences.javaScriptCanOpenWindowsAutomatically = NO;
        
    WKWebView *webView = [[WKWebView alloc]initWithFrame:self.view.frame configuration:config];
    
    • WKUserContentController
    // 内容交互控制器,该对象提供了通过js向web view发送消息的途径
    WKUserContentController *userContentController = [[WKUserContentController alloc] init];
    //添加在js中操作的对象名称,通过该对象来向web view发送消息
    [userContentController addScriptMessageHandler:self name:@"FirstJsObect"];
        
    // 设置WKWebView配置
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];
    config.userContentController = userContentController;
    
    WKWebView *webview = [[WKWebView alloc]initWithFrame:self.view.bounds configuration:config];
    
    • WKWebsiteDataStore
      iOS 9.0以后才能使用这个类。是代表webView不同的数据类型,cookiesdiskmemory cachesWebSQLIndexedDB数据库和本地存储。版本适配的化就要放弃了。
    // 默认数据存储
    + (WKWebsiteDataStore *)defaultDataStore;
     
    // 返回非持久化存储,数据不会写入文件系统
    + (WKWebsiteDataStore *)nonPersistentDataStore;
     
    // 只读属性,表示是否是持久化存储
    @property (nonatomic, readonly, getter=isPersistent) BOOL persistent;
     
    // 获取所有web内容的数据存储类型集,比如cookies、disk等
    + (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;
    
    导航代理
    • WKNavigationDelegate
    @protocol WKNavigationDelegate <NSObject>
     
    @optional
     
    // 决定导航的动作,通常用于处理跨域的链接能否导航。WebKit对跨域进行了安全检查限制,不允许跨域,因此我们要对不能跨域的链接
    // 单独处理。但是,对于Safari是允许跨域的,不用这么处理。
    // 这个是决定是否Request
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
     
    // 决定是否接收响应
    // 这个是决定是否接收response
    // 要获取response,通过WKNavigationResponse对象获取
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler;
     
    // 当main frame的导航开始请求时,会调用此方法
    - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation;
     
    // 当main frame接收到服务重定向时,会回调此方法
    - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation;
     
    // 当main frame开始加载数据失败时,会回调
    - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;
     
    // 当main frame的web内容开始到达时,会回调
    - (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation;
     
    // 当main frame导航完成时,会回调
    - (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation;
     
    // 当main frame最后下载数据失败时,会回调
    - (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;
     
    // 这与用于授权验证的API,与AFN、UIWebView的授权验证API是一样的
    - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *__nullable credential))completionHandler;
     
    // 当web content处理完成时,会回调
    - (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView NS_AVAILABLE(10_11, 9_0);
     
    @end
    
    • WKNavigationResponse
    // WKNavigationResponse是导航响应类,通过它可以获取相关响应的信息:
    NS_CLASS_AVAILABLE(10_10, 8_0)
    @interface WKNavigationResponse : NSObject
     
    // 是否是main frame
    @property (nonatomic, readonly, getter=isForMainFrame) BOOL forMainFrame;
     
    // 获取响应response
    @property (nonatomic, readonly, copy) NSURLResponse *response;
     
    // 是否显示MIMEType
    @property (nonatomic, readonly) BOOL canShowMIMEType;
     
    @end
    
    • WKNavigationAction
    // WKNavigationAction对象包含关于导航的action的信息,用于make policy decisions
    
    // 正在请求的导航的frame
    @property (nonatomic, readonly, copy) WKFrameInfo *sourceFrame;
    // 目标frame,如果这是新的window,它会是nil
    @property (nullable, nonatomic, readonly, copy) WKFrameInfo *targetFrame; 
    // 导航类型,如下面的小标题WKNavigationType
    @property (nonatomic, readonly) WKNavigationType navigationType;
    // 导航的请求
    @property (nonatomic, readonly, copy) NSURLRequest *request;
    
    用户交互代理
    @protocol WKUIDelegate <NSObject>
     
    @optional
     
    // 创建新的webview
    // 可以指定配置对象、导航动作对象、window特性
    - (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures;
     
    // webview关闭时回调
    - (void)webViewDidClose:(WKWebView *)webView NS_AVAILABLE(10_11, 9_0);
     
    // 调用JS的alert()方法
    - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler;
     
    // 调用JS的confirm()方法
    - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler;
     
    // 调用JS的prompt()方法
    - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler;
     
    @end
    
    前进或者后退的页面列表
    • WKBackForwardList
    NS_CLASS_AVAILABLE(10_10, 8_0)
    @interface WKBackForwardList : NSObject
     
    // 当前正在显示的item(页面)
    @property (nullable, nonatomic, readonly, strong) WKBackForwardListItem *currentItem;
     
    // 后一页,如果没有就是nil
    @property (nullable, nonatomic, readonly, strong) WKBackForwardListItem *backItem;
     
    // 前一页,如果没有就是nil
    @property (nullable, nonatomic, readonly, strong) WKBackForwardListItem *forwardItem;
     
    // 根据下标获取某一个页面的item
    - (nullable WKBackForwardListItem *)itemAtIndex:(NSInteger)index;
     
    // 可以进行goback操作的页面列表
    @property (nonatomic, readonly, copy) NSArray<WKBackForwardListItem *> *backList;
     
    // 可以进行goforward操作的页面列表
    @property (nonatomic, readonly, copy) NSArray<WKBackForwardListItem *> *forwardList;
     
    @end
    
    • WKBackForwardListItem
    NS_CLASS_AVAILABLE(10_10, 8_0)
    @interface WKBackForwardListItem : NSObject
     
    // 该页面的URL
    @property (readonly, copy) NSURL *URL;
     
    // 该页面的title
    @property (nullable, readonly, copy) NSString *title;
     
    // 初始请求该item的请求的URL
    @property (readonly, copy) NSURL *initialURL;
     
    @end
    
    其他
    // 前进或者后退到某一页面
    - (nullable WKNavigation *)goToBackForwardListItem:(WKBackForwardListItem *)item;
     
    // 页面的标题,支持KVO的
    @property (nullable, nonatomic, readonly, copy) NSString *title;
     
    // 当前请求的URL,支持KVO的
    @property (nullable, nonatomic, readonly, copy) NSURL *URL;
     
    // 标识当前是否正在加载内容中,支持KVO的
    @property (nonatomic, readonly, getter=isLoading) BOOL loading;
     
    // 当前加载的进度,范围为[0, 1]
    @property (nonatomic, readonly) double estimatedProgress;
     
    // 标识页面中的所有资源是否通过安全加密连接来加载,支持KVO的
    @property (nonatomic, readonly) BOOL hasOnlySecureContent;
     
    // 当前导航的证书链,支持KVO
    @property (nonatomic, readonly, copy) NSArray *certificateChain NS_AVAILABLE(10_11, 9_0);
     
    // 是否可以招待goback操作,它是支持KVO的
    @property (nonatomic, readonly) BOOL canGoBack;
     
    // 是否可以执行gofarward操作,支持KVO
    @property (nonatomic, readonly) BOOL canGoForward;
     
    // 返回上一页面,如果不能返回,则什么也不干
    - (nullable WKNavigation *)goBack;
     
    // 进入下一页面,如果不能前进,则什么也不干
    - (nullable WKNavigation *)goForward;
     
    // 重新载入页面
    - (nullable WKNavigation *)reload;
     
    // 重新从原始URL载入
    - (nullable WKNavigation *)reloadFromOrigin;
     
    // 停止加载数据
    - (void)stopLoading;
     
    // 执行JS代码
    - (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ __nullable)(__nullable id, NSError * __nullable error))completionHandler;
     
    // 标识是否支持左、右swipe手势是否可以前进、后退
    @property (nonatomic) BOOL allowsBackForwardNavigationGestures;
     
    // 自定义user agent,如果没有则为nil
    @property (nullable, nonatomic, copy) NSString *customUserAgent NS_AVAILABLE(10_11, 9_0);
     
    // 在iOS上默认为NO,标识不允许链接预览
    @property (nonatomic) BOOL allowsLinkPreview NS_AVAILABLE(10_11, 9_0);
     
    #if TARGET_OS_IPHONE
    /*! @abstract The scroll view associated with the web view.
     */
    @property (nonatomic, readonly, strong) UIScrollView *scrollView;
    #endif
     
    #if !TARGET_OS_IPHONE
    // 标识是否支持放大手势,默认为NO
    @property (nonatomic) BOOL allowsMagnification;
     
    // 放大因子,默认为1
    @property (nonatomic) CGFloat magnification;
     
    // 根据设置的缩放因子来缩放页面,并居中显示结果在指定的点
    - (void)setMagnification:(CGFloat)magnification centeredAtPoint:(CGPoint)point;
    

    对WKWebView属性的监听

    // 添加KVO监听
    [self.webView addObserver:self
                   forKeyPath:@"loading"
                      options:NSKeyValueObservingOptionNew
                      context:nil];
        
    [self.webView addObserver:self
                   forKeyPath:@"title"
                      options:NSKeyValueObservingOptionNew
                      context:nil];
        
    [self.webView addObserver:self
                   forKeyPath:@"estimatedProgress"
                      options:NSKeyValueObservingOptionNew
                      context:nil];
    
    - (void)dealloc {
        // 移除kvo
        [self.webView removeObserver:self forKeyPath:@"loading" context:nil];
        [self.webView removeObserver:self forKeyPath:@"title" context:nil];
        [self.webView removeObserver:self forKeyPath:@"estimatedProgress" context:nil];
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath
                          ofObject:(id)object
                            change:(NSDictionary<NSString *,id> *)change
                           context:(void *)context {
        if ([keyPath isEqualToString:@"loading"]) {
            NSLog(@"loading");
        } else if ([keyPath isEqualToString:@"title"]) {
            self.title = self.webView.title;
        } else if ([keyPath isEqualToString:@"estimatedProgress"]) {
            NSLog(@"progress: %f", self.webView.estimatedProgress);
            self.progressView.progress = self.webView.estimatedProgress;
        }
        
        // 加载完成
        if (!self.webView.loading) {
            [UIView animateWithDuration:0.5 animations:^{
                self.progressView.alpha = 0.0;
            }];
        }
    }
    

    相关文章

      网友评论

          本文标题:WKWebView的使用

          本文链接:https://www.haomeiwen.com/subject/hynetftx.html