内容字号:默认大号超大号

段落设置:段首缩进取消段首缩进

字体设置:切换到微软雅黑切换到宋体

iOS WKWebView H5微信支付跳转

2018-11-07 18:01 出处:清屏网 人气: 评论(0

需求:iOS客户端实现嵌入H5进行微信支付跳转到微信客户端,支付完成后再跳转回我们的APP,解决WKWebView无法跳转回APP的BUG.

阅读前提:

  • 了解WKWebView基本初始化及使用
  • 了解如何利用URL Schemes进行应用间跳转
  • 公司或个人已经在微信后台注册了一级域名

GitHub地址(附代码) : iOS WKWebView H5微信支付跳转

简书地址 : iOS WKWebView H5微信支付跳转

博客地址 : iOS WKWebView H5微信支付跳转

掘金地址 : iOS WKWebView H5微信支付跳转

注意:如对WKWebView基本的初始化代理方法不了解可自行百度谷歌,解释众多,本文只针对了解基础后实现。

1.在项目的Info.plist中添加一个URL Schemes(用于跳回我们项目),如下图所示

URL Schemes

其中我们只要关心URL Schemes中item的名字例如 xdx.xiaodongxie.cn

  • xdx 为前缀,可任意填写
  • xiaodongxie.cn为你们公司在微信后台注册的一级域名

注意:xiaodongxie.cn一级域名不可随意填写,否则网页将报错:商家参数格式错误,请联系商家解决.

2.添加WKNavigationDelegate代理,并实现重定向代理方法

3.通过拦截微信链接以实现跳转

3-1. 拦截微信支付地址

前缀 https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb 的网址,该网址即为进行微信支付

3-2. 解决完成微信支付后无法跳转回APP

拦截后我们需要首先关注在原始地址中是否含有redirect_url=字符串,如果含有该字符串则说明你们后台人员是利用该字符串在微信支付完成后跳转到支付完成的界面.而我们也需要利用该字段以实现支付完成后跳转回我们的APP.

  • 如果包含redirect_url=字段,我们需要先记住后台重定向的地址,然后将其替换成我们配置好的URL schemes以实现跳转回我们的APP.然后在跳转回我们APP之后我们会手动再加载一次原先重定向的URL地址。
  • 如果不包含redirect_url=字段,我们只需要添加该字段到原始URL最后面即可

3-3. 使用 [[UIApplication sharedApplication] openURL:request.URL]; 即可打开微信客户端

注意

https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    
    NSURLRequest *request        = navigationAction.request;
    NSString     *scheme         = [request.URL scheme];
    NSString     *absoluteString = navigationAction.request.URL.absoluteString;
    NSLog(@"Current URL is %@",absoluteString);
    
    static NSString *endPayRedirectURL = nil;
    
    // Wechat Pay, Note : modify redirect_url to resolve we couldn't return our app from wechat client.
    if ([absoluteString hasPrefix:@"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb"] && ![absoluteString hasSuffix:[NSString stringWithFormat:@"redirect_url=xdx.%@://",CompanyFirstDomainByWeChatRegister]]) {
        decisionHandler(WKNavigationActionPolicyCancel);
        
#warning Note : The string "xiaodongxie.cn://" must be configured by wechat background. It must be your company first domin. You also should configure "URL types" in the Info.plist file.
        
        // 1. If the url contain "redirect_url" : We need to remember it to use our scheme replace it.
        // 2. If the url not contain "redirect_url" , We should add it so that we will could jump to our app.
        NSString *redirectUrl = nil;
        if ([absoluteString containsString:@"redirect_url="]) {
            endPayRedirectURL = [self getParamValueByName:@"redirect_url" URLString:absoluteString];
            redirectUrl = [self modityParamValueByName:@"redirect_url" newValue:[NSString stringWithFormat:@"xdx.%@://",CompanyFirstDomainByWeChatRegister] URLString:absoluteString];
        }else {
            redirectUrl = [absoluteString stringByAppendingString:[NSString stringWithFormat:@"&redirect_url=xdx.%@://",CompanyFirstDomainByWeChatRegister]];
        }
        
        NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:redirectUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:XDX_URL_TIMEOUT];
        newRequest.allHTTPHeaderFields = request.allHTTPHeaderFields;
        newRequest.URL = [NSURL URLWithString:redirectUrl];
        [webView loadRequest:newRequest];
        return;
    }
    
    // Judge is whether to jump to other app.
    if (![scheme isEqualToString:@"https"] && ![scheme isEqualToString:@"http"]) {
        decisionHandler(WKNavigationActionPolicyCancel);
        if ([scheme isEqualToString:[NSString stringWithFormat:@"xdx.%@",CompanyFirstDomainByWeChatRegister]]) {
            
        }
        
        // The var endPayRedirectURL was our saved origin url's redirect address. We need to load it when we return from wechat client.
        if (endPayRedirectURL) {
            NSString *decodeURL = [endPayRedirectURL stringByRemovingPercentEncoding];
            [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:decodeURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:XDX_URL_TIMEOUT]];
        }
        
        [[UIApplication sharedApplication] openURL:request.URL];
        return;
    }
    
    decisionHandler(WKNavigationActionPolicyAllow);
}
分享给小伙伴们:
本文标签: iOSWKWebView微信支付

相关文章

发表评论愿您的每句评论,都能给大家的生活添色彩,带来共鸣,带来思索,带来快乐。

CopyRight © 2015-2016 QingPingShan.com , All Rights Reserved.

清屏网 版权所有 豫ICP备15026204号