WKWebView

WKWebView

// 这个是决定是否Request
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
//选择性打开url scheme,编码url
    NSString *urlStr = [navigationAction.request.URL.absoluteString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    if ([urlStr hasPrefix:@"alipays://"] || [urlStr hasPrefix:@"itms-apps://"]) {
        urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:urlStr]]) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
        }else {
            [MBProgressHUD showMessage:@"请先安装支付宝"];
        }
    }
//  在发送请求之前,决定是否跳转
    decisionHandler(WKNavigationActionPolicyAllow);
}

进度条

__weak typeof(self) weakSelf = self;
    [RACObserve(self.webView, estimatedProgress) subscribeNext:^(id x) {
        [weakSelf.progressView setAlpha:1.0f];
        [weakSelf.progressView setProgress:weakSelf.webView.estimatedProgress];
        
        if(weakSelf.webView.estimatedProgress >= 1.0f) {
            [UIView animateWithDuration:0.3f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
                [weakSelf.progressView setAlpha:0.0f];
            } completion:^(BOOL finished) {
                [weakSelf.progressView setProgress:0.f];
            }];
        }
    }];

控制手势返回

// 是否允许手势左滑返回上一级, 类似导航控制的左滑返回
@property (nonatomic) BOOL allowsBackForwardNavigationGestures;

[RACObserve(self.webView, canGoBack) subscribeNext:^(id x) {
        if (weakSelf.webView.canGoBack == YES) {
            weakSelf.navigationController.interactivePopGestureRecognizer.enabled = NO;
        }else {
            weakSelf.navigationController.interactivePopGestureRecognizer.enabled = YES;
        }
        
    }];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。