WKWebView not loading webpage - renders blank screen in Swift

后端 未结 5 1862
渐次进展
渐次进展 2020-12-11 00:48

This code is supposed to load the Apple homepage, but instead shows a blank screen.
This happens with both HTTP and HTTPS URLs.

Code:

import UIK         


        
5条回答
  •  無奈伤痛
    2020-12-11 01:51

    The correct way with auto layout and constant 10 for all device:

    import UIKit
    import WebKit
    
    class ViewController: UIViewController, WKUIDelegate {
    
    let webView = WKWebView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupWebView()
    }
    
    fileprivate func setupWebView() {
        webView.uiDelegate = self
        webView.translatesAutoresizingMaskIntoConstraints = false
        DispatchQueue.main.async {
            guard let url = URL(string: "http://www.apple.com") else { return }
            self.webView.load(URLRequest(url: url))
        }
        view.addSubview(webView)
        webView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
        webView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
        webView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
        webView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 10).isActive = true
    }}
    

    in info.plist:

    NSAppTransportSecurity
    
        NSAllowsArbitraryLoads
        
    
    

提交回复
热议问题