WKWebView的简单使用

真的是超级简单的是使用哟:

(1)先加一个私有方法:便于直接调用,如果你只是单纯的显示一个HTML,这个就可以了

#pragma mark - Private Methods

- (void)loadWebView:(WKWebView *)webView withURL:(NSURL *)url

{

    DDLogDebug(@"<%@> %@ ", NSStringFromClass(self.class), NSStringFromSelector(_cmd));

    if (!url || ![UIApplication.sharedApplication canOpenURL:url]) {

        return;

    }

    [webView loadRequest:[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:[NSURLRequest new].timeoutInterval]];

}

(2)如果是在加载网页的时候加上什么loading啊这些,就要delegate啦:

 self.WebView.navigationDelegate = self;

    self.WebView.UIDelegate = self;

 

(3)delegate:

#pragma mark WKUIDelegate

 

- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures

{

    if (![navigationAction.targetFrame isMainFrame]) {

        [self loadWebView:webView withURL:navigationAction.request.URL];

    }

    return nil;

}

 

- (void)webViewDidClose:(WKWebView *)webView {

    NSLog(@"webViewDidClose");

}

 

#pragma mark WKNavigationDelegate

-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{

    

    for (NSString *itunesLinkPrefix in kItunesLinkPrefixList) {

        if ([navigationAction.request.URL.absoluteString hasPrefix:itunesLinkPrefix]) {

            DISMISS_LOADING

            break;

        }

    }

    

//    if (self.validatedRequest || navigationAction.navigationType != WKNavigationTypeLinkActivated) {

        decisionHandler(WKNavigationActionPolicyAllow);

//    }

    self.originalUrl = navigationAction.request.URL;

//    [self loadWebView:webView withURL:self.originalUrl];

}

 

- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation {

    SHOW_LOADING;

    NSLog(@"load start ");

}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {

    DISMISS_LOADING;

    NSLog(@"load finish");

    self.validatedRequest = NO;

    //

        NSString *currentURL = self.originalUrl.absoluteString;

        if (currentURL) {

            if (![self.documentWebViewHistoryList.lastObject isEqualToString:currentURL]) {

                [self.documentWebViewHistoryList addObject:currentURL];

            }

        }

    

        DDLogDebug(@"<%@> %@ URL: %@\ndocumentWebViewHistoryList: %@", NSStringFromClass(self.class), NSStringFromSelector(_cmd), currentURL, self.documentWebViewHistoryList);

    

        if ([self.documentWebViewHistoryList count] > 1) {

            [self.goBackButton setEnabled:YES];

        }

        else {

            [self.goBackButton setEnabled:NO];

        }

    

        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];

        [[NSUserDefaults standardUserDefaults] synchronize];

}

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {

    DDLogDebug(@"<%@> %@ ", NSStringFromClass(self.class), NSStringFromSelector(_cmd));

        NSLog(@"URL loading fail,%@",error.debugDescription);

    //    DISMISS_LOADING

    //    [self showAlertView];

    

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值