Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView not loading webpage - renders blank screen in Swift

Tags:

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 UIKit
import WebKit

class WebViewController: UIViewController, WKUIDelegate{

    var webView: WKWebView!

    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView

    }
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .red

        let myURL = URL(string: "https://www.apple.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }
}

I have tried changing 'App Transport Security Settings' to allow arbitrary load. Didn't change anything.

Screenshots:

View hierarchy:

Captured view hierarchy Screenshot

Safari debug console for the simulator:

Safari debug console Screenshot

enter image description here

like image 954
Shrikant Chidgopkar Avatar asked May 11 '18 11:05

Shrikant Chidgopkar


2 Answers

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.

Safari screenshot

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

System log

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.

like image 122
Shrikant Chidgopkar Avatar answered Sep 28 '22 10:09

Shrikant Chidgopkar


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.

enter image description here


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/>
like image 21
Mick Avatar answered Sep 28 '22 09:09

Mick