如何实现:
1、针对iOS9增加白名单
2、设置自己项目的URL Scheme,这个必须是H5授权的域名
3、实现代码
#import "TuningupPaymentController.h"
@interface TuningupPaymentController () <UIWebViewDelegate>
@property (nonatomic,strong) UIWebView *webView;
@end
@implementation TuningupPaymentController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self setLeftBackButton];
self.webView = [UIWebView new];
[self.view addSubview:self.webView];
[self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
self.webView.delegate = self;
self.webView.scrollView.bounces = NO;
NSURL *url = [NSURL URLWithString:self.urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
NSMutableURLRequest *mutableRequest = [request mutableCopy];
[mutableRequest setValue:@"http://tp.htaonet.com" forHTTPHeaderField:@"Referer"];//设置商户申请H5时提交的授权域名
[self.webView loadRequest:mutableRequest];
}
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//拦截微信下单请求
NSString *urlStr = request.URL.absoluteString;
if ([urlStr rangeOfString:@"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?"].location != NSNotFound && ![urlStr containsString:@"t.htaonet.com://"]) {
//在这个请求的基础上生成新的请求
urlStr = [urlStr stringByReplacingOccurrencesOfString:@"redirect_url=http://t.htaonet.com/" withString:@"redirect_url=t.htaonet.com://"];
NSURL *newUrl = [NSURL URLWithString:urlStr];
NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:newUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[newRequest setValue:@"t.htaonet.com://" forHTTPHeaderField:@"Referer"];//设置授权域名
[webView loadRequest:newRequest];
return NO;
}
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[HELPER showProgressHUDWithText:@"加载中" inView:self.view];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[HELPER hideHUDForView:self.view];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[HELPER hideHUDForView:self.view];
[self setTitleViewTitle:[webView stringByEvaluatingJavaScriptFromString:@"document.title"]];
}
@end
另外,如果APP中还有H5支付宝支付的话,需要在支付完成后返回APP只需要再加一层判断即可:
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *urlStr = request.URL.absoluteString;
if ([urlStr rangeOfString:@"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?"].location != NSNotFound && ![urlStr containsString:@"t.htaonet.com://"]) {
//在这个请求的基础上生成新的请求
urlStr = [urlStr stringByReplacingOccurrencesOfString:@"redirect_url=http://t.htaonet.com/" withString:@"redirect_url=t.htaonet.com://"];
NSURL *newUrl = [NSURL URLWithString:urlStr];
NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:newUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[newRequest setValue:@"t.htaonet.com://" forHTTPHeaderField:@"Referer"];//设置授权域名
[webView loadRequest:newRequest];
[self foundFinishPayingUI];
return NO;
}else if ([urlStr rangeOfString:@"alipay://alipayclient"].location != NSNotFound) {//拦截支付宝URL
urlStr = [self URLDecodeString:urlStr];
NSRange range = [urlStr rangeOfString:@"{"];
NSString *jsonStr = [urlStr substringFromIndex:range.location];
//JSON字符串转字典
NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
NSMutableDictionary *mutableDict = dict.mutableCopy;
[mutableDict setObject:@"duu" forKey:@"fromAppUrlScheme"];
//字典转JSON字符串
NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:mutableDict.copy options:NSJSONWritingPrettyPrinted error:nil];
NSString *tempStr = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
//对转成的JSON字符串进行URL编码
NSString *encodedString = (NSString*) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)tempStr, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8));
urlStr = [@"alipay://alipayclient/?" stringByAppendingString:encodedString];
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr] options:@{} completionHandler:nil];
} else {
// Fallback on earlier versions
}
return NO;
}
return YES;
}
#pragma mark - URL解码
- (NSString *)URLDecodeString:(NSString *)str{
NSString *decodeString = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
return decodeString;
}