WKWebView not loading webpage - renders blank screen in Swift

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

    Building an App for the Mac?

    If you are building an app for the Mac, make sure to turn App Sandbox ON in your target and tick Networks Outgoing Connections.


    Update: If you prefer the long way, add the following to YourAppName.entitlements

    <key>com.apple.security.app-sandbox</key>
    <true/>
    <key>com.apple.security.network.client</key>
    <true/>
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-11 01:42

    I have found the solution. The problem was being caused by AVG AntiVirus's webshield. For some reason AVG webshield treats all network communication from the simulator as fraudulent. The following screenshot shows the safari app running on simulator. It says that www.apple.com is not safe or any other website.

    The following screenshot is from system.log showing errors with webkit.

    You can replicate this problem by installing AVG antivirus and turning on the webshield. WKWebview in your App(On the simulator) wouldn't load anything.

    I don't understand why it wasn't working on an actual device tho. It could have been another problem with the device. I also deleted the derived data folder, the actual app and had restarted the device.

    Thank you everybody for the answers and help.

    0 讨论(0)
  • 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:

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
    
    0 讨论(0)
  • 2020-12-11 01:51

    The following code works just fine for me in Xcode 9.3:

    import UIKit
    import WebKit
    
    class ViewController: UIViewController {
    
        var webView: WKWebView!
    
        override func loadView() {
            webView = WKWebView(frame: .zero, configuration: WKWebViewConfiguration())
            self.view = webView
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            webView.load(URLRequest(url: URL(string: "https://www.apple.com")!))
        }
    
    }
    

    Here's the result:

    0 讨论(0)
提交回复
热议问题