全是干货,不过多废话.
1.首先需要调用接口,因为H5微信链接接口请求成功后会返回
干货一:请求参数需要外网IP给后台,外网IP的获取
搜了一些资料,讲的全是内网IP的获取.
//获取外网IP
NSURL *ipURL = [NSURL URLWithString:@"http://ip.taobao.com/service/getIpInfo2.php?ip=myip"];
NSData *data = [NSData dataWithContentsOfURL:ipURL];
NSDictionary *ipDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSString *ipStr = nil;
if (ipDic && [ipDic[@"code"] integerValue] == 0) { //获取成功
ipStr = ipDic[@"data"][@"ip"];
}
NSLog(@"ipStr == %@",ipStr);
接口返回H5微信支付的链接,调用WebView打开
干货二:打开链接显示商家参数格式有误,请联系商家解决
这里需要设置webView的Referer
_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, gScreenWidth, gScreenHeight)];
_webView.delegate = self;
[self.view addSubview:_webView];
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSDictionary *headers = [request allHTTPHeaderFields];
BOOL hasReferer = [headers objectForKey:@"Referer"] != nil;
if (hasReferer) {
// .. is this my referer?
} else {
// relaunch with a modified requesthttps://upload-images.jianshu.io/upload_images/12287987-d8e15aab7a556d69.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setValue:@"buyhoo.cc://" forHTTPHeaderField: @"Referer"];
[_webView loadRequest:request];
});
});
}
只是这样还是不行的,不能调起微信H5支付
最后一步,需要设置URL Types
刚刚Referer设置的什么,去掉://就是需要填在这里的
这样就能成功的调起微信H5支付了
喜欢的点个赞吧~