Getting Blank Screen in WebView using Swift

2019-09-14 23:49发布

问题:

I am making a simple webview using Swift 3. But after run am getting only the blank screen.

It is not opening the link which I put in url request.

import UIKit

class ViewController: UIViewController {

@IBOutlet var webView: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    let url = URL (string: "http://www.google.net");
    let request = URLRequest(url: url!);
    webView.loadRequest(request);
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

回答1:

You have to set the NSAllowsArbitraryLoads key to YES under NSAppTransportSecurity dictionary in your .plist file.

Like this Image:



回答2:

write this code in info.plist

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

and write this code in viewdid load

webView.opaque = false
webView.backgroundColor = UIColor.clear


回答3:

webView.loadRequest(request as URLRequest)

try this it might help..