iOS开发-WKWebView加载微信H5支付

在开发中,遇到需要使用WKWebView加载微信H5支付

一、使用前配置

使用WKWebView加载微信H5支付,需要在info.plist文件中配置CFBundleURLTypes

<dict>
		<key>CFBundleTypeRole</key>
		<string>Editor</string>
		<key>CFBundleURLName</key>
		<string>laileshuo.com</string>
		<key>CFBundleURLSchemes</key>
		<array>
			<string>laileshuo.com</string>
		</array>
	</dict>

配置LSApplicationQueriesSchemes

weixin
wechat
weixinULAPI

二、使用WKWebView加载微信H5支付

我们需要在decidePolicyForNavigationAction方法中决定是否需要跳转。

#pragma mark - WKNavigationDelegate
/**
 *  在发送请求之前,决定是否跳转
 *
 *  @param webView          实现该代理的
 *  @param navigationAction 当前navigation
 *  @param decisionHandler  是否调转block
 */
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    
    DebugLog(@"%s", __FUNCTION__);
    DebugLog(@"decidePolicyForNavigationActionrequest:%@\nhttpMethod:%@\nbody:%@\nheader:%@",navigationAction.request,navigationAction.request.HTTPMethod,navigationAction.request.HTTPBody,navigationAction.request.allHTTPHeaderFields);
    
    NSString *url = navigationAction.request.URL.absoluteString;
    DebugLog(@"url:%@",url);
    
    NSURLRequest*request = navigationAction.request;
    
    NSString *dfaceUrl = @"laileshuo.com://";
    if ([url hasPrefix:dfaceUrl] && !self.hasLoadWXCallbackUrl) {
        NSString *aUrl = [[url substringFromIndex:dfaceUrl.length] URLDecodedString];
        
        // 打开组装的laileshuo.com://${redirect_url},获取${redirect_url},使用webview打开
        NSMutableURLRequest *aUrlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:aUrl]];
        DebugLog(@"aUrl:%@;aUrlRequest:%@",aUrl,aUrlRequest);

        self.hasLoadWXCallbackUrl = YES;
        self.hasLoadWXPre = NO;
        
        [self.browserView.webView loadRequest:aUrlRequest];
        
        decisionHandler(WKNavigationActionPolicyCancel);
        return;
    }

    NSString *wxPre = @"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb";

    if([request.URL.absoluteString hasPrefix:wxPre] && !self.hasLoadWXPre) {
        NSArray *urlArray = [url componentsSeparatedByString:@"redirect_url="];
        if (urlArray.count >= 2) {
            // 从url中 获取redirect_url的内容
            // 之后将其组装成laileshuo.com://${redirect_url} 在url编码

            NSString *a = [urlArray firstObject];
            NSString *b = [urlArray objectAtIndex:1];
            DebugLog(@"a:%@;b:%@",a,b);
            NSString *bURLEncode = [[NSString stringWithFormat:@"%@%@",@"laileshuo.com://",b] stringByURLEncode];
            NSString *newURLStr = [NSString stringWithFormat:@"%@redirect_url=%@",a,bURLEncode];
            
            // NSString *referer = [request.allHTTPHeaderFields objectForKey:@"Referer"];
            
            NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:request.allHTTPHeaderFields];
            [dict setObject:@"pay.laileshuo.com" forKey:@"Referer"];

            NSMutableURLRequest *anewRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:newURLStr]];

            anewRequest.allHTTPHeaderFields = dict;
            
            self.hasLoadWXCallbackUrl = NO;

            self.hasLoadWXPre = YES;
            
            [self.browserView.webView loadRequest:anewRequest];

            decisionHandler(WKNavigationActionPolicyCancel);
            return;
        }
    }

    
    if ([[DFSessionManager shareInstance] hasLogin]) {
        if (url && [DFRouterConfigManager shareInstance].urlPatternPrefix && [url hasPrefix:[DFRouterConfigManager shareInstance].urlPatternPrefix]) {
        
            NSString *urlPatternPrefix = [DFRouterConfigManager shareInstance].urlPatternPrefix;
            NSString *tmpUrl = [url substringFromIndex:urlPatternPrefix.length];
            NSArray *tmpUrls = [tmpUrl componentsSeparatedByString:@"?"];
            NSString *controllerUrl = nil;
            if (tmpUrls && tmpUrls.count > 0) {
                controllerUrl = [tmpUrls firstObject];
            }

            NSDictionary *userInfo = [url getURLParameters];
            DebugLog(@"url params:%@",userInfo);
            DebugLog(@"controllerUrl:%@",controllerUrl);
            if (controllerUrl && controllerUrl.length > 0) {
                [[DFRouterManager sharedInstance] routerController:DFPageRouterStylePush URL:controllerUrl userInfo:userInfo handler:^(NSDictionary *routerParameters) {
                    DebugLog(@"callback routerParameters:%@",routerParameters);
                }];
            }
            decisionHandler(WKNavigationActionPolicyCancel);
        }
    }
    //调起微信等
    if ([url containsString:@"weixin://"]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
        decisionHandler(WKNavigationActionPolicyCancel);
        return;
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}

三、小结

使用WKWebView加载微信H5支付。在此记录一下。

学习记录,每天不停进步。

你可能感兴趣的:(移动开发,iphone开发,Objective-c,ios,WKWebView,微信,H5支付)