Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading a URL with a WKWebView in macOS

I've made a fresh mac app project with the following code in the ViewController.

import Cocoa
import WebKit

class ViewController: NSViewController {

    var loginWebView: WKWebView!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.loginWebView = WKWebView(frame: self.view.frame)

        self.view.addSubview(loginWebView)

        let urlReq = URLRequest(url: URL(string: "https://apple.com/")!)

        self.loginWebView.load(urlReq)


        // Do any additional setup after loading the view.
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }
}

For some reason when I launch the app the web view isn't loading the website. Why is this? I've been developing on iOS for years and am just getting started with macOS.

EDIT: Update loading a local file wouldn't work either

self.loginWebView.loadFileURL(fileURL, allowingReadAccessTo: dirURL)
like image 374
Big_Mac Avatar asked Nov 24 '17 03:11

Big_Mac


1 Answers

The problem was that I had not enabled Incoming and Outgoing Connections in my app's sandboxing settings. I discovered this by reverting to using the legacy WebView and then getting an error code in the console. WKWebKit will not print error messages making something really simple incredibly difficult and confusing!. Apple really needs to fix that

like image 60
Big_Mac Avatar answered Oct 05 '22 13:10

Big_Mac