iOS 集成一网通支付的坑

1 篇文章 0 订阅

1.UIWebView 和 WKWebView必须都实现, 因为 SDK 中会在有 WKWebView 的时候自动调用 WKWebView 的方法, 如果不实现 WKWebView 会造成崩溃;

2. UIWebView 发送请求

    //1.设置请求路径
    NSURL *URL=[NSURL URLWithString:@"一网通支付接口地址"];

    //2.创建请求对象
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:URL];
    
    request.timeoutInterval=5.0;//设置请求超时为5秒
    
    request.HTTPMethod=@"POST";//设置请求方法
    
    //设置请求体
    NSString *jsonRequestData = [NSString stringWithFormat:@"jsonRequestData=%@", [@"这里是通过后台获取的数据字典(参照文档里的数据结构)" JSONString]];
    request.HTTPBody=[jsonRequestData dataUsingEncoding:NSUTF8StringEncoding];
    
    [_webView loadRequest: request];

3. WKWebView 发送请求

因为 WKWebView 存在的 bug, 不能直接像 UIWebView一样直接发送 post 参数 (具体参考:http://www.jianshu.com/p/403853b63537)

我使用的是 通过 JavaScript 解决

js 代码如下(假如命名为:JSPOST.html):

 <html>
 <head>
     <script>
         //调用格式: post('URL', {"key": "value"});
         function post(path, params) {
             var method = "post";
             var form = document.createElement("form");
             form.setAttribute("method", method);
             form.setAttribute("action", path);

            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", "jsonRequestData");
            hiddenField.setAttribute("value", params);

            form.appendChild(hiddenField);
            
            document.body.appendChild(form);
            form.submit();
         }
     </script>
 </head>
 <body>
 </body>
</html>

OC 实现代码

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[CMBWebKeyboard shareInstance] hideKeyboard];
    
    // 获取JS所在的路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"JSPOST" ofType:@"html"];
    // 获得html内容
    NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    // 加载js
    [self.webView loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];
}

// 加载完成的代理方法
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    // 判断是否需要加载(仅在第一次加载)
    if (self.needLoadJSPOST) {
        // 调用使用JS发送POST请求的方法
        [self postRequestWithJS];
        // 将Flag置为NO(后面就不需要加载了)
        self.needLoadJSPOST = NO;
        
    }     
}

// 调用JS发送POST请求
- (void)postRequestWithJS {
    // 发送POST的参数
    NSString *postData = [NSString stringWithFormat:@"%@", [@"这里是通过后台获取的数据字典(参照文档里的数据结构)" JSONString]];
    // 请求的页面地址
    NSString *urlStr = @"一网通支付接口地址";
    NSString *jscript = [NSString stringWithFormat:@"post('%@', '%@');", urlStr, postData];
    
    // 调用JS代码
    [self.webView evaluateJavaScript:jscript completionHandler:nil];
}


其他坑网上有了解决方案, 我就不写了.

还有其他的问题欢迎大家交流

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值