UIWebview 加载 URL 后仍显示空白白屏幕

4

我有一个HTML页面,在提交按钮点击时,我需要调用付款网关的URL。
我正在使用NSMutableURLRequest,通过传递post数据来调用URL。
请查看以下代码:

NSDictionary *parameters = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:currency_code,apikey,merchant_id,checksum_method,authorize_user,ip_address,hash,NB, nil] forKeys:[NSArray arrayWithObjects:@"currency_code",@"apikey",@"merchant_id",@"checksum_method",@"authorize_user",@"ip_address",@"checksum",@"cardType", nil]];
__block NSString *post = @"";
[parameters enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    if ([post isEqualToString:@""]) {
        post = [NSString stringWithFormat:@"%@=%@",key,obj];
    } else {
        post = [NSString stringWithFormat:@"%@&%@=%@",post,key,obj];
    }
}];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%ld",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://pay.sandbox.shmart.in/pay_merchant/js"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

[webview loadRequest:request];
webViewDidFinishLoad也被调用了,但是它显示给我一个空白的白屏。你有任何想法是什么问题呢?

请尝试在Safari浏览器中点击链接并查看结果。 - Reshmi Majumder
4个回答

4

由于不安全,传输安全已经阻止了明文HTTP(http://)资源加载。您需要在.plist文件中的NSAppTransportSecurity字典下添加NSAllowsArbitraryLoads键并将其设置为YES,然后尝试查看此链接


@Roshni - 你能展示一下你的URL吗?如果你的URL有误或不可见,你能更新一下代码吗? - Anbu.Karthik
@Roshni - 尝试隐藏 NSString *postLength = [NSString stringWithFormat:@"%ld",[postData length]];[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; - Anbu.Karthik
你能展示一下 post 是什么,同时看一下这个链接关于 NSURL 的问题吗?http://stackoverflow.com/questions/15291197/nsurl-always-returns-nil - Anbu.Karthik
authorize_user=1&state=MAHARASHTRA&f_name=xyzcccddd&amount=26750&mobileNo=9673870771&currency_code=INR&checksum_method=SHA256&checksum=e4b844fbee5c4ed69e2670c489&cardType=NB - Roshni
这是一个NSString的帖子,我也检查了URL。它不会变成nil。 - Roshni

1
我曾经遇到相同的问题,并通过从URL中删除空格和不必要的字符来解决它。
如果您只尝试加载HTTP URL,请检查并在info.plist中启用App Transport Security Settings中的AllowArbitaryLoad。 您可以参考以下链接。 传输安全已阻止明文HTTP 如果您正在使用https URL,请确保Safari和Chrome浏览器响应模式下加载URL正常。
有时,网站会在iFrame中加载响应URL。 因此,请检查是否传递了正确的URL。
此外,在传递给UIWebView之前,请打印URL,有时它可能包含空格以及&。 请删除它们并将其传递给WebView。
serviceBookingUrl = [NSURLRequest requestWithURL: [NSURL URLWithString:[websiteURL stringByReplacingOccurrencesOfString:@"&" withString:@"&"]]];
[PrimaryWebView loadRequest: serviceBookingUrl];

其中 websiteURL 是网站的 URL。


0

enter image description here

按照图片添加,确保在Safari浏览器中网址正确

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

没有,我没有任何背景颜色。虽然我将其设置为clearColor,但它并没有显示出来。 - Roshni
尝试设置白色背景颜色。 - wg_hjl

0

你的代码应该像这样:

  UIWebView *tempWebview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 300, 500)];

[self.view addSubview:tempWebview];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];

[tempWebview loadRequest:request];

您的网址必须支持移动Web浏览器

因此,请检查您的网址和参数是否有效,并且您已经发出了有效的请求。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接