10

I am trying to build a simple WKWebView application that will display Google in the WebView. The app works perfectly fine; however, upon implementing the WKWebView, the app no longer shows a window. It just simply launches the app (in the dock) with no NSView in sight.

ViewController.swift

import Cocoa
import WebKit

class ViewController: NSViewController {

    var webView: WKWebView?

    override func loadView() {
        self.webView = WKWebView()
        self.view = self.webView!

        let url = NSURL(string:"http://www.google.com/")
        let req = NSURLRequest(URL: url!)
        self.webView!.loadRequest(req)
    }

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

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


}

There doesn't seem to be any further examples online, nor does there seem to be any projects on GitHub.

Thanks. I haven't come across this before.

JDev
  • 5,168
  • 6
  • 40
  • 61

2 Answers2

43

I guess there're some limitations behind Xcode Settings those hidden settings make your WKWebView NOT WORK!!!

You could try these ways, Maybe you will find the solution:

1) Search these key words by Google, You'll find some tutorials:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

2) Check your Project Setting panel. Make sure you've CHECKED the NetWork [Incoming] & [OutComing] item:

ScreenShot

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
Corxit Sun
  • 618
  • 6
  • 8
  • 1
    One thing I noticed, it appears you only need the second part (Incoming and outcoming in App Sandbox) after you have started using automatic provisioning and tied the app to iTunes. Before doing that, it seems to work without those sandbox settings. – Typewriter Nov 23 '17 at 23:58
  • Thanks! Doing this, it works. But only running thought Xcode. If I run by opening the "app" generated it doesn't load anything, and not giving errors. – Frade Oct 11 '18 at 11:28
  • I only needed the `Outgoing Connections (Client)` portion to get `WKWebView` to work. I left the other unchecked because I don't plan on my app being a server. There's more documentation on it [here](https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/EnablingAppSandbox.html). – Mr Rogers Dec 17 '18 at 18:32
  • Just spent about 15 minutes pulling my hear out at this to realise I just needed to check the incoming and outgoing connections in the sandbox config.... This should really be highlighted in the console, similar to not having the correct privacy string fields set for features such as location services or camera usage. – Stewart Thomson Jun 02 '19 at 19:20
  • Perfect, thanks... setting that client is always a stumbling block! – Jc Nolan Feb 04 '20 at 04:04
  • Please everybody upvote this answer, just saved me from going insane after trying to get WKWebView to work for half a day. – vfxdev Mar 30 '20 at 15:17
  • Thank you! Needed to tick the boxes only – SwiftiSwift Jan 11 '21 at 22:45
0

With Xcode 14 I needed the 'outgoing connection' and additionally the frame:

var webView: WKWebView!

override func loadView() {
    // these two lines are important:
    super.loadView()
    webView = WKWebView(frame:self.view.frame)

    view = webView
    
    let url = URL(string:"https://www.example.com/")!
    let req = URLRequest(url: url)
    webView.load(req)
    
    // webView.loadHTMLString("<html><body bgcolor=red><p>Hello, World!</p></body></html>", baseURL: nil)
}
shim
  • 9,289
  • 12
  • 69
  • 108
dirkk0
  • 2,460
  • 28
  • 34