WKWebView not loading webpage - renders blank screen in Swift

后端 未结 5 1846
渐次进展
渐次进展 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:30

    You can try below code

    import UIKit
    import WebKit
    
    class WebViewController: UIViewController {
    
    var wkWebview: WKWebView!
    
     override func viewDidLoad() {
    
       super.viewDidLoad()
    
        var Ycord : CGFloat = 0.0 // for top space 
        if UIScreen.main.bounds.height == 812 { //Check for iPhone-x
            Ycord = 44.0
        }
        else {
            Ycord = 20.0
        }
    
        let frame = CGRect(x: 0.0, y: Ycord, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height-Ycord)
    
        self.wkWebview = WKWebView(frame: frame, configuration: WKWebViewConfiguration())
        self.wkWebview.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        self.wkWebview.uiDelegate = self
        self.view.addSubview(self.wkWebview)
    
        DispatchQueue.main.async {
    
            self.wkWebview.load(URLRequest(url:URL(string: "https://www.apple.com")!))
        }        
     }
    }
    

    Output

提交回复
热议问题