iOS wkwebview中无法调起支付宝/微信客户端支付问题

2019-01-17  本文已影响0人  不泯iOS

这两个的解决思路都是要在下面这个方法中先拦截相应的url,再单独处理

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;

支付宝

在我的项目中拦截到的url样式如下:
alipay://alipayclient/?{"dataString":"h5_route_token=\"bf7ace48916875f26c94fba6f62226ef\"&is_h5_route=\"true\"","requestType":"SafePay","fromAppUrlScheme":"alipays"}
具体代码如下:

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    NSString*urlString = navigationAction.request.URL.absoluteString;
    urlString = [urlString stringByRemovingPercentEncoding];
    if ([urlString containsString:@"alipay://"]){//alipay://alipayclient/
        //拦截到之后不允许跳转
        decisionHandler(WKNavigationActionPolicyCancel);
        //截取到的是参数拼成的json字符串
        NSString* dataStr=[urlString substringFromIndex:23];
        //将json字符串转化成字典
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[dataStr dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
        /**因为我本身项目中就已经集成过支付宝支付的sdk,所以我就试了一下看能否直接用他们的sdk唤起支付宝客户端,结果证明可以,还顺便解决了返回原app的问题。
         payOrder     --->   订单信息
         fromScheme   --->   填写你自己app的scheme,这样可以解决,支付后返回到本app的问题。
         */
        [[AlipaySDK defaultService] payOrder:dict[@"dataString"] fromScheme:@"yourAppScheme" callback:^(NSDictionary *resultDic) {
            
        }];
    } else{
        decisionHandler(WKNavigationActionPolicyAllow);
    }

}

微信

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    NSString*urlString = navigationAction.request.URL.absoluteString;
    urlString = [urlString stringByRemovingPercentEncoding];
    if ([urlString containsString:@"weixin://wap/pay?"]) {
        decisionHandler(WKNavigationActionPolicyCancel);
        //解决wkwebview weixin://无法打开微信客户端的处理
        NSURL *url = [NSURL URLWithString:urlString];
        if (@available(iOS 10.0, *)) {
            [[UIApplication sharedApplication]openURL:url options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @NO} completionHandler:^(BOOL success) {
            }];
        } else {
            // Fallback on earlier versions
        }
    }else{
        decisionHandler(WKNavigationActionPolicyAllow);
    }

}
上一篇下一篇

猜你喜欢

热点阅读