【问题标题】:UIWebview showing blank white screen even after loading urlUIWebview 即使在加载 url 后也显示空白屏幕
【发布时间】:2016-07-27 05:17:30
【问题描述】:

我有一个 html 页面,点击提交按钮时,我需要调用支付网关 URL。
我正在使用NSMutableURLRequest 并传递了发布数据,我正在使用
调用 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 上点击链接并查看结果

标签: ios objective-c uiwebview


【解决方案1】:

传输安全已阻止明文 HTTP (http://) 资源加载,因为它不安全,您需要添加 You have to set theNSAllowsArbitraryLoadskey toYESunderNSAppTransportSecuritydictionary in your.plist 文件`。,然后尝试看到这个link

【讨论】:

  • @Roshni - 你能显示你的 url,如果你的 url 错误是不可见的,你能更新代码吗
  • @Roshni - 隐藏 NSString *postLength = [NSString stringWithFormat:@"%ld",[postData length]]; 和 ``[request setValue:postLength forHTTPHeaderField:@"Content-Length"];` 尝试一次
  • 你能显示什么是post,同时看到这个链接一次 NSURL stackoverflow.com/questions/15291197/nsurl-always-returns-nil
  • authorize_user=1&state=MAHARASHTRA&f_name=xyzcccddd&amount=26750&mobileNo=9673870771&currency_code=INR&checksum_method=SHA256&checksum=e4b844fbee5c4ed69e2670c489&cardType=NB
  • 这是 NSString 帖子,我也检查了网址。它不会为零。
【解决方案2】:

我遇到了同样的问题,并通过从 URL 中删除空格和不需要的字符来修复它。

如果您尝试仅加载 HTTP URL,请在 info.plist 的 App Transport Security Settings 中检查并启用 AllowArbitaryLoad。 您可以参考以下链接。

Transport security has blocked a cleartext HTTP

如果您使用的是 https 网址,请确保该网址在 Safari 和 Chrome 浏览器响应模式下都能正常加载。

有时,网站会在 iFrame 中加载响应 URL。所以请检查是否传递了正确的 URL。

另外,在传递给 UIWebView 之前打印 URL,有时它应该有空格以及 &。所以删除相同的并在 WebView 中传递它。

serviceBookingUrl = [NSURLRequest requestWithURL: [NSURL URLWithString:[websiteURL stringByReplacingOccurrencesOfString:@"&" withString:@"&"]]];
[PrimaryWebView loadRequest: serviceBookingUrl];

其中 websiteURL 是网站 URL。

【讨论】:

    【解决方案3】:

    你的代码应该是这样的,

      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];
    

    Your url must support mobile web browser

    所以请检查您的 url 和参数是否有效,并且您提出了有效的请求。

    【讨论】:

      【解决方案4】:

      添加喜欢的图片,确保你的url在safair中是ok的

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

      【讨论】:

      • 没有。我没有任何背景颜色。虽然我将它设置为 clearColor,但它没有显示出来。
      • 尝试设置白色背景色
      猜你喜欢
      • 2017-01-13
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2014-07-30
      • 2017-12-01
      • 2019-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多