美文网首页
UIWebView和WKWebView比较

UIWebView和WKWebView比较

作者: coder小鹏 | 来源:发表于2017-01-08 13:04 被阅读293次

    1.UIWebView的优缺点

    <1>UIWebView由于系统本身存在的问题可能导致内存泄漏;
    <2>UIWebView支持缓存和NSURLProtocol 拦截;
    <3>用UIWebView加载大数据资源时,内存峰值、内存都会暴增,同时CPU电量消耗也较高.

    2.WKWebView的优缺点

    <1>WKWebView网页加载速度有提升,更快(占用内存可能只有 UIWebView 的1/3~1/4);
    <2>提供加载网页进度的属性;
    <3>没有缓存,对UIWebViewDelegate中的方法拆分的更为细致了,如果对于缓存不是很高的页面,可以采用WKWebView,能够有更好的用户体验;
    <4>WKWebView不支持缓存和NSURLProtocol 拦截;
    <5>拥有高达60FPS滚动刷新率及内置手势;
    <6>高效的app和web信息交换通道

    3.WKWebView常用API

    容器相关

    //配置
    @property (nonatomic, readonly, copy) WKWebViewConfiguration *configuration;
    //页面标题,支持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;
    //是否可以执行GoBack操作,支持KVO
    @property (nonatomic, readonly) BOOL canGoBack;
    //是否可以执行GoFoward操作,支持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;
    

    页面加载相关

    //从request加载资源
    - (nullable WKNavigation *)loadRequest:(NSURLRequest *)request;
    //从本地文件加载(只支持IOS9.0以上)
    - (nullable WKNavigation *)loadFileURL:(NSURL *)URL allowingReadAccessToURL:(NSURL *)readAccessURL 
    //从HTML字符串加载
    - (nullable WKNavigation *)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;
    //从二进制数据流中加载
    - (nullable WKNavigation *)loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL NS_AVAILABLE(10_11, 9_0);
    

    代理相关

    //在发送请求之前,决定是否跳转
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
    //在收到服务器的响应头,根据response相关信息,决定是否跳转,decisionHandler必须调用,来决定是否跳转,参数
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler;
    //接收到服务器跳转请求后调用(服务器端redirect),不一定调用
    - (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;
    //SSL认证
    - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *__nullable credential))completionHandler;
    //页面内容完全加载完
    - (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView NS_AVAILABLE(10_11, 9_0);
    

    上面只是简单介绍了一下WKWebView的常用方法,如果还有进一步的需求,请参考http://www.jianshu.com/p/90a90bd13aac ,或者私信我,微信号1395258209.

    相关文章

      网友评论

          本文标题:UIWebView和WKWebView比较

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