iOS中内嵌WKWebView,通过H5调起支付宝客户端支付

1.由于苹果审核,对于应用内应该使用内购功能,却没有使用内购,使用了第三方支付的,一律不给通过,广大开发者就开启各种脑洞,内嵌H5就是其中一种智慧的结晶,然鹅,支付宝相关文档比较滞后,相关资料也参差不齐,坑点比较多。下面最坑的一个点就是,H5支付的时候,调起支付宝客户端不起作用,原因是逻辑写错地方了,正确的处理如下:

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    if (webView != _webView) {return;}
    
    NSString *urlStr = navigationAction.request.URL.absoluteString;
    if ([urlStr hasPrefix:@"alipays://"] || [urlStr hasPrefix:@"alipay://"]) {
        
        NSURL *alipayURL = [NSURL URLWithString:urlStr];
        
        if (@available(iOS 10.0, *)) {
            [[UIApplication sharedApplication] openURL:alipayURL options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @NO} completionHandler:^(BOOL success) {
                
            }];
        } else {
            // Fallback on earlier versions
            [[UIApplication sharedApplication] openURL:alipayURL];
        }
    }
    
    decisionHandler(WKNavigationActionPolicyAllow);
}

2.写在下面几个方法中并不能调起支付宝客户端

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler;

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation;

你可能感兴趣的:(iOS中内嵌WKWebView,通过H5调起支付宝客户端支付)