ios App微信H5支付不能跳转和返回App 事件处理

1:首先是h5不能唤醒微信的事件处理

代理事件如下 逻辑处理如下

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

 

 

2:wkwebview打开微信返回app回调处理

+    NSString *url = navigationAction.request.URL.absoluteString;
+    if ([url containsString:@"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?"]) {
+        //传入的是微信支付链接:https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx201801291021026cb304f9050743178155&package=3456576571
+        //这里把webView设置成一个像素点,主要是不影响操作和界面,主要的作用是设置referer和调起微信
+        
+        WebChatPayH5VIew *h5View = [[WebChatPayH5VIew alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
+        //url是没有拼接redirect_url微信h5支付链接
+        [h5View loadingURL:url withIsWebChatURL:NO];
+        [self.view addSubview:h5View];
         decisionHandler(WKNavigationActionPolicyCancel);
+    } else {
+        decisionHandler(WKNavigationActionPolicyAllow);
     }

 

       其中WebChatPayH5VIew为uiwebview  里面主要逻辑为如下

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            dispatch_async(dispatch_get_main_queue(), ^{

                NSURL *url = [request URL];

                NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

                //设置授权域名

                [request setValue:@"www.payment.xy189.cn://" forHTTPHeaderField: @"Referer"];

                [self.myWebView loadRequest:request];

            });

        });

demo:https://github.com/mjl123iOS/iOS_WXh5

你可能感兴趣的:(iOS)