22

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

Shrikant Chidgopkar
  • 695
  • 1
  • 6
  • 13
  • 1
    Your code works fine. I tried it in demo. I think it might be other issue. – Khushbu May 11 '18 at 11:53
  • I haven't used any storyboard files in my app. Its all code. – Shrikant Chidgopkar May 11 '18 at 11:56
  • When is `loadView` called? Is it before or after `viewDidLoad`? I don't see it called anywhere, and that's where you actually create your web view – mag_zbc May 11 '18 at 11:57
  • Yes the code is from Apple website, but don't know why i cannot get it to work. Its the first time I am using WKWebView. – Shrikant Chidgopkar May 11 '18 at 11:57
  • Yes. as you did it works fine. try only this code in one demo. – Khushbu May 11 '18 at 11:58
  • loadView is called before viewDidLoad – Shrikant Chidgopkar May 11 '18 at 12:00
  • Just because the code for `loadView` is written before `viewDidLoad`, doesn't make it automatically get _called_ before. –  May 11 '18 at 12:04
  • @user770 This code is from the Apple's official developer guide for WkWebView – Shrikant Chidgopkar May 11 '18 at 12:08
  • And they say that loadView is called first? –  May 11 '18 at 12:08
  • @user770 yes looks like that. https://developer.apple.com/documentation/uikit/uiviewcontroller/1621454-loadview – Shrikant Chidgopkar May 11 '18 at 12:12
  • I don't know if this is the only way, but I added a webkit view in storyboard, make a outlet for it `@IBOutlet weak var webView: WKWebView!` , removed your `webView` var and replaced it with this outlet (line 7 I think), and it works. –  May 11 '18 at 12:16
  • @user770 My app doesn't use any storyboard or nib files at all. Its all in code. What should i do? – Shrikant Chidgopkar May 11 '18 at 12:21
  • How does Xcode know which view to load first? The blank screen isn't because the page is not loading, it's because the **view** isn't. Try getting the screen to say "hello" using just code, and we'll take it from there. –  May 11 '18 at 12:23
  • @user770 View is definitely loading, see the screenshot for captured view hierarchy. I put the background colour as red. I did also put breakpoint on both methods and they are executing for sure. – Shrikant Chidgopkar May 11 '18 at 12:27
  • Did you manage to get "hello" on the screen by adding code to the same file we're working with? –  May 11 '18 at 12:30
  • I have the same issues, WKWebView works on device, but not Simulator. However, UIWebView works in the Simulator. I have BitDefender installed on my Mac, could that be what is causing WKWebView to not work in the Sim? – Sajjon Nov 01 '18 at 12:14
  • It could be. Open any iPhone simulator and launch safari app on the simulator, if you are not able to load any websites on the simulator’s safari, there is a good chance that the culprit might be Bitdefender or some similar software installed on the computer blocking the network – Shrikant Chidgopkar Nov 02 '18 at 15:15
  • Here is the answer to a question with a similar problem! https://stackoverflow.com/questions/44285901/cant-load-my-web-view-with-an-http-url – farabiDev Jul 17 '19 at 09:29

5 Answers5

15

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/>
Mick
  • 30,759
  • 16
  • 111
  • 130
13

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.

Shrikant Chidgopkar
  • 695
  • 1
  • 6
  • 13
  • nailed it! its antivirus damn! – dip Apr 16 '19 at 11:13
  • 1
    Just a quick note. I ran into this problem and I got the same as in his screenshot but it was because I was on our company's VPN. If I got off of VPN then it showed the web page. – Mark Moeykens Aug 09 '21 at 20:05
7

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>
Fabio
  • 5,432
  • 4
  • 22
  • 24
6

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:

WKWebView in iOS Simulator.

Sam Fischer
  • 1,442
  • 19
  • 35
0

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

enter image description here

Rohit Makwana
  • 4,337
  • 1
  • 21
  • 29