美文网首页
iOS WKWebView 加载支付宝地址,打开支付宝APP

iOS WKWebView 加载支付宝地址,打开支付宝APP

作者: 听歌么 | 来源:发表于2022-03-11 11:58 被阅读0次
    
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
        
        NSURLRequest *request        = navigationAction.request;
        NSString     *scheme         = [request.URL scheme];
        // decode for all URL to avoid url contains some special character so that it wasn't load.
        NSString     *absoluteString = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
        NSLog(@"Current URL is %@",absoluteString);
        static NSString *endPayRedirectURL = nil;
        
        // 跳转到本地某宝App
        if ([absoluteString hasPrefix:@"alipays://"] || [absoluteString hasPrefix:@"alipay://"])
        {
            NSURL *openedURL = navigationAction.request.URL;
            
            NSString *prefixString = @"alipay://alipayclient/?";
            NSString *urlString = [[self sht_URLDecodedString:absoluteString] stringByReplacingOccurrencesOfString:@"alipays" withString:@"jxtapp://"];
            ;
            if ([urlString hasPrefix:prefixString]) {
                NSRange rang = [urlString rangeOfString:prefixString];
                NSString *subString = [urlString substringFromIndex:rang.length];
                NSString *encodedString = [prefixString stringByAppendingString:[self sht_URLEncodedString:subString]];
                openedURL = [NSURL URLWithString:encodedString];
            }
            BOOL isSucc = [[UIApplication sharedApplication] openURL:openedURL];
            if (!isSucc) {
                [ToastShow showErrorMessage:@"未安装支付宝,请选择其他验证方式!"];
            }
            decisionHandler(WKNavigationActionPolicyCancel);
            return;
        }
        
        decisionHandler(WKNavigationActionPolicyAllow);
    }
     
    - (NSString *)sht_URLDecodedString:(NSString *)urlString
    {
        NSString *string = urlString;
        NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)string, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
        return decodedString;
    }
     
    - (NSString *)sht_URLEncodedString:(NSString *)urlString
    {
        NSString *string = urlString;
        NSString *encodedString = (NSString *) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                                                         (CFStringRef)string,
                                                                                                         NULL,
                                                                                                         (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                                                         kCFStringEncodingUTF8));
        return encodedString;
    }
    
    

    原文链接:https://blog.csdn.net/SHTLoveXX/article/details/108469266

    相关文章

      网友评论

          本文标题:iOS WKWebView 加载支付宝地址,打开支付宝APP

          本文链接:https://www.haomeiwen.com/subject/odvcdrtx.html