《 返回 2017-05-04 17:25:05

第三方APP通过webview无法调起微信支付、支付宝支付解决方案

当开发者将“游戏推广地址”通过webview嵌入APP中,但用户无法调起客户端支付功能。


因为默认webview只能识别http://或https://开头的url, 因此如果要识别其他的scheme (如: alipays://、weixin://等等),我们就要自行处理。



Android解决方法一:


WebView设置WebViewClient类,并重写shouldOverrideUrlLoading()方法


Android完整代码如下:


WebViewClient webViewClient = new WebViewClient() {
@Override
     public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("weixin://wap/pay?")) {
                      Intent intent = new Intent();
                      intent.setAction(Intent.ACTION_VIEW);
                      intent.setData(Uri.parse(url));
                      aty.startActivity(intent);
                      return true;
                 }else if (parseScheme(url)) {
                      try {
                           Intent intent = Intent.parseUri(url,Intent.URI_INTENT_SCHEME);
                           intent.addCategory("android.intent.category.BROWSABLE");
                           intent.setComponent(null);
                           aty.startActivity(intent);
                           return true;
                      } catch (Exception e) {
                           e.printStackTrace();
                     }
                 }
         return false;
     }

     private boolean parseScheme(String url) {
          url=url.toLowerCase(Locale.getDefault());
          if (url.contains("platformapi/startapp")) {
               return true;
          } else if ((Build.VERSION.SDK_INT > 23) && (url.contains("platformapi") && url.contains("startapp"))) {
               return true;
          } else {
               return false;
           }
     }
}
 
webview.setWebViewClient(webViewClient);


Android解决方法二:


通过调起手机自带的浏览器加载“游戏推广地址”。


Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(“游戏推广地址”));
startActivity(intent);


IOS解决方法一:


使用UIWebView跳转微信支付,在UIWebViewDelegate的webViewDidStartLoad方法中监测微信支付URL。


代码如下:


- (void)webViewDidStartLoad:(UIWebView *)webView{
 NSURL *url = webView.request.URL;//获取当前加载的URL
  if ([urlString containsString:@"weixin://wap/pay?"]) {//判断是否包含微信支付链接
          UIWebView *webV = [[UIWebView alloc]init];//使用隐藏的UIWebView,自动唤起微信支付
          [webV loadRequest:[NSURLRequest requestWithURL:url]];
          webV.hidden = YES;
          [self.view addSubview:webV];
          return;
    }
}


IOS解决方法二:


使用WKUIWebView调起微信支付,在WKNavigationDelegate的decidePolicyForNavigationAction方法中监测微信支付的URL。


代码如下:


- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    NSURL *url = navigationAction.request.URL;//获取当前加载的URL
       if ([urlString containsString:@"weixin://wap/pay?"]) {//判断是否包含微信支付链接
        UIWebView *webV = [[UIWebView alloc]init];//使用隐藏的UIWebView,自动唤起微信支付
        [webV loadRequest:[NSURLRequest requestWithURL:url]];
        webV.hidden = YES;
        [self.view addSubview:webV];
        decisionHandler(WKNavigationActionPolicyCancel);
        return;
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}


如有问题请联系我们技术团队 QQ:766561365


温馨提示 关闭