WKWEBVIEW替换UIWEBVIEW

-(WKWebView *)webView {
    if (!_webView) {
    //自适应JS语句
        NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
        WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        WKUserContentController *wkUController = [[WKUserContentController alloc] init];
        [wkUController addUserScript:wkUScript];
        WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
        wkWebConfig.userContentController = wkUController;
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(CGFLOAT_MIN, CGFLOAT_MIN, self.webBaseView.w, self.webBaseView.h) configuration:wkWebConfig];
        _webView.navigationDelegate = self;
        [self.webBaseView addSubview:_webView];
    }
    return _webView;
}
#pragma mark - WKWebView Delegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    // 类似 UIWebView 的 -webView: shouldStartLoadWithRequest: navigationType:
    if ([@"http" isEqualToString:navigationAction.request.URL.scheme]) {
        UIStoryboard *mainBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
        OrderWebViewController *orderWebViewController = [mainBoard instantiateViewControllerWithIdentifier:@"OrderWebViewController"];
        orderWebViewController.loadUrl = navigationAction.request.URL.absoluteString;
        [self.navigationController pushViewController:orderWebViewController animated:YES];
        decisionHandler(WKNavigationActionPolicyCancel);
        return;
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}

你可能感兴趣的:(WKWEBVIEW替换UIWEBVIEW)