UIWebview在加载url后显示空白的白色屏幕

时间:2016-09-06 10:16:38

标签: ios objective-c uiwebview uiwebviewdelegate

以下是我尝试的代码:

 HTMLBrowserViewController.h
 IBOutlet UIWebView * webControl;

 HTMLBrowserViewController.m

 webControl.scalesPageToFit = YES;

webControl.contentMode = UIViewContentModeScaleAspectFit;

webControl.delegate = self;

NSURL *url1=[NSURL URLWithString:webUrl];

NSURLRequest *requestObject=[NSURLRequest requestWithURL:url1 cachePolicy:NSURLCacheStorageAllowed timeoutInterval:15];    

[webControl loadRequest:requestObject];

3 个答案:

答案 0 :(得分:1)

在您的应用.plist文件中添加NSAppTransportSecurity

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

Check this link transport Security has Blocked a cleartext HTTP

答案 1 :(得分:1)

如果仅尝试加载HTTP URL,请在info.plist的“应用传输安全性设置”中检查并启用AllowArbitaryLoad。您可以参考以下链接。

Transport security has blocked a cleartext HTTP

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

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

另外,在传递给UIWebView之前,先打印URL,有时它应该有空格,并且&并因此将其删除并传递到WebView中。

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

答案 2 :(得分:0)

试试这个:

  NSString *encodedString=[urlString       stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSLog(@"urlString %@",encodedString);

    NSURL *url = [NSURL URLWithString:encodedString];

    //URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    // Set delegete
    webView.delegate=self;

    //Load the request in the UIWebView.
    [webView loadRequest:requestObj];