IOS 关于支付宝web支付不同实现

由于支付宝web支付返回的是一个带请求链接的表单,所以直接加载html形式,不要使用loadrequest

[self.webView loadHTMLString:self.requestUrlStrs baseURL:nil];

使用WKwebView方式遵守WKNavigationDelegate协议,建议使用WKwebView因为UIWebView会导致内存泄漏

-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{



    NSString *url = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
    NSString* reUrl=[[webView URL] absoluteString];
    reUrl = url;

    if ([url containsString:@"alipay://"]) {
        NSInteger subIndex = 23;
        NSString* dataStr=[url substringFromIndex:subIndex];
        //编码
        NSString *encodeString = [self encodeString:dataStr];
        NSMutableString* mString=[[NSMutableString alloc] init];
        [mString appendString:[url substringToIndex:subIndex]];
        [mString appendString:encodeString];

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mString]];

}
    decisionHandler(WKNavigationActionPolicyAllow);
    return;
}
-(NSString*)encodeString:(NSString*)unencodedString{

        // CharactersToBeEscaped = @":/?&=;+!@#$()~',*";

        // CharactersToLeaveUnescaped = @"[].";

        NSString *encodedString = (NSString *)

        CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,

                                                                  (CFStringRef)unencodedString,

                                                                  NULL,

                                                                  (CFStringRef)@"!*'();:@&=+$,/?%#[]",

                                                                  kCFStringEncodingUTF8));

        return encodedString;

    }

使用webview方式,遵守webviewdelegate 协议,建议使用WKwebView因为UIWebView会导致内存泄漏

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    // NOTE: ------  对alipays:相关的scheme处理 -------
    // NOTE: 若遇到支付宝相关scheme,则跳转到本地支付宝App
    NSString* reqUrl = request.URL.absoluteString;
    if ([reqUrl hasPrefix:@"alipays://"] || [reqUrl hasPrefix:@"alipay://"]) {
        // NOTE: 跳转支付宝App,bSucc判断是否成功
        BOOL bSucc = [[UIApplication sharedApplication]openURL:request.URL];
    }
    return YES;
}

IOS 关于web支付无法返回APP解决方案

你可能感兴趣的:(IOS,IOS开发)