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