简体   繁体   中英

iOS app with UIWebView gives blank screen

I am new to iOS development and xCode and wanted to make a simple app containing a fullscreen webview.

I found the following tutorial: webview tutorial

I followed the steps mentioned and when I ran the app in the simulator, it just gave me a blank screen.

I then downloaded the source code, ran that and I still get a blank screen. So something tells me it has nothing to do with the code, but maybe with some sort of configuration? As far as I can tell I have a WiFi connection as the wifi icon is in my status bar in the top of the screen. If necessary, I will happily post any relevant code.

Thanks!

It is happening because you are trying to send http request and App transport security of iOS allows only https request unless you explicitly specify in the plist. You can either filter particular domain from the above check using

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>yourdomain.com</key>
        <dict>
            <!--Include to allow subdomains-->
            <key>NSIncludesSubdomains</key>
            <true/>
            <!--Include to allow HTTP requests-->
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <!--Include to specify minimum TLS version-->
            <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>TLSv1.1</string>
        </dict>
    </dict>
</dict>

Or you can disable entire transport security by adding following to your info.plist file.

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

Instead of "http://..." use a "https://" URL. IOS has a relatively new restriction that forbids loading of unsecured URLs, which must be removed by the "AllowArbitaryLoads=YES" key in the Info.plist. (Create the key "App Transport Security", unfold it and create the ArbitaryLoads one with the BOOL value YES)

UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width ,self.view.frame.size.height)];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
[webview loadRequest:nsrequest];
[self.view addSubview:webview];

Try this, definitely helps to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM