iOS WKWebView的基本使用

导语:

对于刚开始接触WKWebView的同鞋们说,会有点陌生,这里简单说下,有一个重要的代理:WKNavigationDelegate,去配置WebView基本的属性

首先是WKWebView的基本配置:
- (DWKWebView *)webView{
    if (_webView == nil) {
        WKPreferences *preferences = [[WKPreferences alloc] init];
        // 打开web访问本地文件权限
        [preferences setValue:@(true) forKey:@"allowFileAccessFromFileURLs"];
        preferences.javaScriptCanOpenWindowsAutomatically = YES;
        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
        if (@available(iOS 13.0, *)) {
            configuration.defaultWebpagePreferences.preferredContentMode = WKContentModeMobile;
        }
        WKUserContentController *userContentController = [[WKUserContentController alloc] init];
        WKUserScript *cookieScript = [[WKUserScript alloc] initWithSource:[self getCookieSource] injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
        [userContentController addUserScript:cookieScript];
        configuration.userContentController = userContentController;
        configuration.preferences = preferences;
        configuration.allowsInlineMediaPlayback = YES;
        configuration.applicationNameForUserAgent = @"selfappinfotonative";
        configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
        _webView = [[DWKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
        _webView.navigationDelegate = self;
        _webView.allowsBackForwardNavigationGestures = YES;
        _webView.scrollView.showsHorizontalScrollIndicator = NO;
        _webView.scrollView.showsVerticalScrollIndicator = NO;
        _webView.scrollView.bounces = NO;
        _webView.opaque = NO;
        JsApiTest *apiTest= [[JsApiTest alloc] initWithaWebVC:self];
        apiTest.signBBlock = ^(NSString * _Nonnull signStr) {
            self.aliPayStr = [NSString stringWithString:signStr];
        };
        [_webView addJavascriptObject:apiTest namespace:nil];
        _webView.backgroundColor = [UIColor whiteColor];
    }
    return _webView;
}

# pragma mark --WKNavigationDelegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    NSLog(@"当网页完成时");
}

- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
    NSLog(@"当内容开始返回时调用");
}

- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
    NSLog(@"接收到服务器跳转请求之后调用");
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
    NSLog(@"在收到响应后,决定是否跳转");
    //允许跳转
    decisionHandler(WKNavigationResponsePolicyAllow);
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    NSLog(@"在发送请求前,决定是否跳转");
    //mark:H5支付WKWebView中无法拉起支付宝/微信客户端支付,需要做如下配置
    //参考网址:https://blog.csdn.net/auccy/article/details/125623087
    //网页检查元素:decodeURIComponent ("abcdefghijklmnopqlstuvwxyz")
    NSURL *url = navigationAction.request.URL;
    NSString *urlStr = [[url absoluteString] stringByRemovingPercentEncoding];
    if ([urlStr containsString:@"weixin://wap/pay"] || [urlStr containsString:@"alipay://alipayclient"]) {
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
        decisionHandler(WKNavigationActionPolicyCancel);
    }else{
        decisionHandler(WKNavigationActionPolicyAllow);
    }
    
}

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

- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView{
    [self.webView reload];
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
    //[PBRouter openURL:message.name andPageParams:nil];
    //JS调用OC方法
    //message.body 就是JS里传过来的参数
    if ([message.name isEqualToString:@"share"]) {
        
    }else if ([message.name isEqualToString:@"Camera"]){
        
    }

}

你可能感兴趣的:(iOS WKWebView的基本使用)