解决H5微信支付返回跳转问题

使用iOS打开微信支付页面需要设置webview的Referer

Referer一般为http://www.xxx.com或者直接www.xxx.com

在Info中URL Type添加该Referer为打开自己app的scheme

嗯,就这样结束!!!

打开微信可能遇到的坑,需添加这个代理,实现部分内容

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    
    NSDictionary *referDict = [[NSUserDefaults standardUserDefaults] objectForKey:@"loadUrlWithRefer"];
    
    if (![ICTools isObjEmpty:referDict]) {
        NSString *url = referDict[@"url"];
        NSString *refer = referDict[@"refer"];
        NSDictionary *headers = [navigationAction.request allHTTPHeaderFields];
        NSString * referer = [headers objectForKey:@"Referer"];
        BOOL isRightReferer = [referer isEqualToString:refer];
        if (isRightReferer) {
            decisionHandler(WKNavigationActionPolicyAllow);
        } else {
            decisionHandler(WKNavigationActionPolicyCancel);

            if ([url isEqualToString:[navigationAction.request.URL absoluteString]]) {
                NSMutableDictionary * muDict = [[NSMutableDictionary alloc] initWithDictionary:headers];
                [muDict setObject:refer forKey:@"Referer"];
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    dispatch_async(dispatch_get_main_queue(), ^{
                        NSURL *url = [navigationAction.request URL];
                        NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
                        request.allHTTPHeaderFields = muDict;
                        [webView loadRequest:request];
                    });
                });
            }
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"loadUrlWithRefer"];// 使用完成后移除
        }
    } else {
        NSURL *url = navigationAction.request.URL;
        ICLog(@"decidePolicyForNavigationAction :%@", [url scheme]);
        NSString *scheme = [url scheme];
        if ([scheme isEqualToString:kTGJSBridgeProtocolScheme]) {// 自定义scheme用于js于native交互使用,正常h5支付可忽略
            [self dispatchNotification:[url absoluteString] fromWebView:webView];
        }  else {
            //forward
            if (![scheme isEqualToString:@"https"] && ![scheme isEqualToString:@"http"]) {
                [[UIApplication sharedApplication] openURL:url];
            }
        }
        decisionHandler(WKNavigationActionPolicyAllow);
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

扫码关注云+社区