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];

    

}

你可能感兴趣的:(WKWebView的简单使用)