由于支付宝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{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{NSString* reqUrl = request.URL.absoluteString;if ([reqUrl hasPrefix:@"alipays://"] || [reqUrl hasPrefix:@"alipay://"]) {BOOL bSucc = [[UIApplication sharedApplication]openURL:request.URL];}return YES;
}
IOS 关于web支付无法返回APP解决方案