5

For some reason, the WKWebView in my app won't display any websites like "https://www.google.com.au". I have tried changing the "Allow Arbitrary Loads" to "YES" in info.plist but that didn't resolve the issue.

PLEASE HELP!!!!! Thanks

My Code:

import UIKit
import WebKit

class ViewController: UIViewController {

    @IBOutlet weak var webView: WKWebView!

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

    private func displayWebPage() {
        let url = URL(string: "https://www.google.com.au")
        let request = URLRequest(url: url!)
        webView.load(request)
    }
}

Screenshot of StoryBoard: StoryBoard

byJeevan
  • 3,728
  • 3
  • 37
  • 60
Matthew Cho
  • 103
  • 4
  • 8
  • Did you ever get this resolved ? If yes, please update it with an answer. I have a similar issue. Thanks! – Curious101 Jun 28 '18 at 05:14
  • No I didn't get it resolved. I had to use code instead of the storyboard. Thanks – Matthew Cho Jun 28 '18 at 05:38
  • In my case that was resolved by NSAppTransportSecurity setting in info.plist. – tokentoken Jul 21 '18 at 02:04
  • Have you checked whether any antivirus enabled n ur system ? I got a similar issue and resolved by turning off my avg antivirus webshield. – Vineeth Sep 29 '18 at 13:56
  • 1
    @Vineeth Yes, that was the problem. Apologies everyone for not updating this post. If the same issue occurs and you've tried everything, like Vineeth said, it's probably because there is some anti-virus webshield active. – Matthew Cho Jan 14 '19 at 02:42

3 Answers3

4

Connect the WKNavigationDelegate, this will fix the webpage not loading try the below code. Any errors thrown while loading the webpage will be printed in the webViewDidFail delegate.

class ViewController: UIViewController, WKNavigationDelegate {

    @IBOutlet weak var webView: WKWebView!

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

    private func displayWebPage() {
        let url = URL(string: "https://www.google.com.au")
        let request = URLRequest(url: url!)
        webView.navigationDelegate = self
        webView.load(request)
    }
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        print(error)
    }
}
BXV
  • 405
  • 3
  • 16
  • 1
    I tried using your code but the screen is still blank (white screen) and no error messages are popping up – Matthew Cho Apr 06 '18 at 05:23
  • Try call `displayWebPage:` after 'ViewWillAppear:Animated' method. – byJeevan Apr 06 '18 at 05:38
  • Are you sure it did work for me though, and please verify the outlets are connected. – BXV Apr 06 '18 at 06:00
  • calling the function after ViewWillAppear doesn't work and yes, my outlets are connected. Is there a possibility that it's not my code that's wrong, but maybe the Xcode Settings or the Simulator settings? – Matthew Cho Apr 06 '18 at 06:09
2

set webView delegates.

    webView.UIDelegate = self
    webView.navigationDelegate = self

and also try by adding your domain in exception domain list

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>google.com.au</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
    </dict>
</dict>
Ajay saini
  • 2,352
  • 1
  • 11
  • 25
0

Connect your controller to WKUIDelegate, add loadView() method in your View Controller and initialize your web view using WKWebViewConfiguration.

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate {

    @IBOutlet weak var webView: WKWebView!
    
    var webView: WKWebView!
    
    override func loadView() {
        let myConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: myConfiguration)
        webView.uiDelegate = self
        view = webView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        displayWebPage()
    }

    private func displayWebPage() {
        let url = URL(string: "https://www.google.com.au")
        let request = URLRequest(url: url!)
        webView.load(request)
    }
}

Source: WK Web View

pinkfox
  • 1
  • 4