iOS开发之调起微信H5支付(包括外网IP获取方法)

全是干货,不过多废话.

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设置的什么,去掉://就是需要填在这里的


刚刚Referer设置的buyhoo.cc://


去掉://就是buyhoo.cc

这样就能成功的调起微信H5支付了

喜欢的点个赞吧~

参考文章:iOS解决微信h5支付无法直接返回app的问题

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容