WKWebView的使用方法:

WKWebView的使用方法:

1.storyboard拉取WKWebView控件,并且将对应的ViewController的Build Version上升到iOS11.0

WKWebView的使用方法:_第1张图片

2. 在对应的ViewController添加

#import

3在Target ---》 Build Phases ---》 加上webKit这个库      ----------《这个是要加的,至少我Xcode11.3.1还没有Fix这个bug》

4.在对应的ViewController做lazy loading,


-(WKWebView *)documentWebView{
    if (!_documentWebView) {
//           WKWebViewConfiguration *configuration = [WKWebViewConfiguration new];
//           WKPreferences *preferences = [WKPreferences new];
//           configuration.preferences = preferences;
//           self.documentWebView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
           _documentWebView.navigationDelegate = self;
           _documentWebView.UIDelegate = self;
           _documentWebView.scrollView.alwaysBounceVertical = NO;
           _documentWebView.translatesAutoresizingMaskIntoConstraints = NO;
       }
       return _documentWebView;
    
    
    
}

添加delegate的主要说明

@protocol WKNavigationDelegate; //类似于UIWebView的加载成功、失败、是否允许跳转等

@protocol WKUIDelegate; //主要是一些alert、打开新窗口之类的

 

5.对应UIWebView的方法

//下面这2个方法共同对应了UIWebView的 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
//先:针对一次action来决定是否允许跳转,action中可以获取request,允许与否都需要调用decisionHandler,比如decisionHandler(WKNavigationActionPolicyCancel);
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
//后:根据response来决定,是否允许跳转,允许与否都需要调用decisionHandler,如decisionHandler(WKNavigationResponsePolicyAllow);
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler;

//开始加载,对应UIWebView的- (void)webViewDidStartLoad:(UIWebView *)webView;
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation;

//加载成功,对应UIWebView的- (void)webViewDidFinishLoad:(UIWebView *)webView;
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation;

//加载失败,对应UIWebView的- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;

 

 

你可能感兴趣的:(WKWebView的使用方法:)