WKWebView

作者: 咔咔尼亜 | 来源:发表于2018-09-07 14:07 被阅读23次
-(WKWebView *)kWebView
{
    if (_kWebView == nil) {
        WKWebViewConfiguration *confige = [WKWebViewConfiguration new];
        confige.suppressesIncrementalRendering = YES;  //决定webview是否等所有数据加载完成之后再渲染视图默认为no,
        confige.processPool = [WKProcessPool new]; //进程池
        confige.preferences = [WKPreferences new]; //偏好设置,主要设置字体大小,JavaScript支持等
        confige.applicationNameForUserAgent = @"设置user-Agent中的应用名字";
        confige.allowsAirPlayForMediaPlayback = YES;//允许远程播放媒体 默认允许
        confige.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;//设置那种媒体需要用户操作才能播放,设置none的时候所有媒体都需要用户操作才能播放
        confige.allowsInlineMediaPlayback = NO;//是否允许小窗口播放,为no是全屏播放video,iphone默认为no,ipad默认为yes
        confige.selectionGranularity = WKSelectionGranularityDynamic; //用户选择内容的间隔,默认是动态的
        confige.allowsPictureInPictureMediaPlayback = YES;//是否允许画中画,默认允许
        confige.dataDetectorTypes = UIDataDetectorTypeLink;//检测类型,数字、链接等
        confige.ignoresViewportScaleLimits = YES;//是否忽略视图scale限制,默认为no
        _kWebView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 66, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-66) configuration:confige];
        _kWebView.navigationDelegate = self;
        _kWebView.UIDelegate = self;
    }
    return _kWebView;
}

需要导入框架

import <WebKit/WebKit.h>

通过懒加载创建WKWebview对象。

WKWebView代理方法

理解wkwebview的代理方法可以的调用很像http请求的三次握手
(理解http网络访问的三次握手,可以模仿两个人的对话,第一次握手:你给别人说话,别人听到了。第二次握手,别人告诉你说,我听到了,第三次握手:你告诉别人说你听到了。)

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
  //是否允许wkwebview中加载带有事件的网页,比如,action事件,submitt事件
    decisionHandler(WKNavigationActionPolicyAllow);
    NSLog(@"decidePolicyForNavigationAction");
}
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation
{
  //开始加载网页调用方法
    NSLog(@"didStartProvisionalNavigation");
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
{
    //是否允许加载网页相当于webview中的shouldLoadWebview方法
    decisionHandler(WKNavigationResponsePolicyAllow);
    NSLog(@"decidePolicyForNavigationResponse");
}
- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation
{

    NSLog(@"didCommitNavigation");
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation
{

    NSLog(@"didFinishNavigation");
}

- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation
{
        NSLog(@"didReceiveServerRedirectForProvisionalNavigation");
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
{
    NSLog(@"didFailProvisionalNavigation");
}

- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
{
    NSLog(@"didFailNavigation");
}

代理方法的执行顺序是从上往下执行的。
wkwebview中的好多属性都是写明了
@discussion @link WKWebView @/link is key-value observing (KVO) compliant
for this property.
意思就是这个属性可以用键值观察,做某些操作,比如estimatedProgress属性标识网页的加载进度,WKWebivew利用WKNavigation的对象来表示网页加载的整个流程,但是这个类的对象官方并没有给出任何相关的属性和方法。
因此上想要监测网页的加载进入只能 利用WKwebview的estimatedProgress属性。

相关文章

网友评论

      本文标题:WKWebView

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