13

before this get's shot down for being a duplicate, it isn't. Pretty much every question on WKWebView here is about WKWebView in iOS Apps, not macOS Apps, with the difference being pretty much just the UIViewController interface being implemented instead of the NSViewController interface in macOS.

The example code in Apple's Documentation as well as the Controller code, that can be found online doesn't work. Altough it does compile without a problem the webview stays inactive.

Is there something that I just didn't see or is this a bug in WKWebView ?
I even copied some code from tutorials showing how to do this for iOS and just changed UIViewController to NSViewController (since that was the ONLY difference), yet it didn't work.

The following code in ViewController.swift does not work. It also doesn't work if it's
class ViewController: NSViewController, WKUIDelegate

import Cocoa;
import WebKit;
class ViewController: NSViewController {
    @IBOutlet weak var webView: WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let url=URL(string: "http://safetec-cam.biz/images/webcam_extern/bad-nauheim_bahnhof_west.jpg");
        webView.load(URLRequest(url: url!));
    }
}

it also doesn't work if it's done like this with the UIViewController exchanged for NSViewController image from https://developer.apple.com/documentation/webkit/wkwebview

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Ozymandias42
  • 131
  • 1
  • 1
  • 5
  • This should work, I'm using something like it. Are you sure the issue is not elsewhere, like with ATS or something network related that would block the request? – Eric Aya Jun 09 '17 at 22:50
  • don't know what you mean by ATS but I do have LittleSnitch (firewall) installed. I tried it whilst having LittleSnitch turned off. No change. Then I tried it with google instead of the webcam-url. Worked immediately. No idea why though. The webcam-link works without a hitch in Firefox. Anyways, thanks for the fast answer. I'll edit the main post to clear that up. – Ozymandias42 Jun 11 '17 at 14:55
  • 1
    I meant [Application Transport Security](https://stackoverflow.com/questions/31254725/transport-security-has-blocked-a-cleartext-http) – Eric Aya Jun 11 '17 at 16:24
  • 1
    ah okay. Thx, for the link. I tried what's suggested there. I added the **NSAppTransportSecurity** dictionary and the **NSAllowsArbitraryLoads** item to the Info.plist. It solved the problem immediately. – Ozymandias42 Jun 16 '17 at 15:02
  • Oh cool! :) Then I suppose you would agree to close your question as a duplicate of the ATS one? That could be useful for people using the same kind of keywords as in your question when they're searching about this. – Eric Aya Jun 16 '17 at 15:04
  • jep. I'll do that. Have to look up how to do it tough. That was my first time actually asking a question here myself^^ – Ozymandias42 Jun 16 '17 at 15:06
  • Ah, I'm doing it myself then, I have authority to do it (but I prefer asking first). Since you agree: let's go. :) – Eric Aya Jun 16 '17 at 15:07

1 Answers1

14

I recommend you to start from scratch:

Set Your URL to be loaded:

let myURLString = "https:yourWebLink"
let url = URL(string: myURLString)
let request = URLRequest(url: url!)

Init and load request in webview:

let webView = WKWebView(frame: self.view.frame)
webView.navigationDelegate = self
webView.load(request)

Implement WKNavigationDelegate to trace your page Load/Error:

extension ViewController: WKNavigationDelegate {

    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        print("Started to load")
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("Finished loading")
    }

    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        print(error.localizedDescription)
    }
}

For further reference check: https://iosdevcenters.blogspot.com/2016/05/creating-simple-browser-with-wkwebview.html

Mushrankhan
  • 749
  • 9
  • 12
Jeba Moses
  • 809
  • 8
  • 25