osx cocoa WKWebview app - swift 4 not working

Hello all,


I'm Michel and just getting acquainted with xcode and swift. I'm trying to get a simple webview to show a website.

And i have troubles for something that looks simple. Maybe its a bug or its something i'm doing wrong. Probably the last.


But with whether i run this code in xcode 8, 9.1 or the last beta.

I see the app but no website. When i run this code (with some modification for ios) its runnen like a charm.

Pure a osx app nothing works.


import Cocoa
import WebKit

class ViewController: NSViewController {

    @IBOutlet weak var mywebview: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
     
        let url = URL(string: "https://www.apple.com")
        let request = URLRequest(url: url!)
        mywebview.load(request)

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


}


I even tried to add WKWebView programmaticly.

I got one working test application where it works but all new demo apps with the same code same as above. result white screen no website.


import Cocoa
import WebKit

class ViewController: NSViewController, WKUIDelegate, WKNavigationDelegate {

    let webView = WKWebView()
   
    override func viewDidLoad() {
        super.viewDidLoad()
       
        self.webView.uiDelegate = self
        self.webView.navigationDelegate = self
        webView.frame = CGRect(x: 0, y: 0, width: 400, height: 270)
        view.addSubview(webView)
       
        let url = URL(string: "https://www.apple.com")
        let request = URLRequest(url: url!)
        webView.load(request)
    }
}



I searched the web for examples, demo apps etc etc. but i find a lot of demo and examples for ios but not for osx.

I hope someone can point me to the right direction.


greetz Michel

Accepted Reply

Aren't you setting the App Sandbox to ON in the Capabilities pane? (It's the default for new projects in Xcode 9.)


If App Sandbox is set to ON, many features would not work unless properly enabled.

Replies

Aren't you setting the App Sandbox to ON in the Capabilities pane? (It's the default for new projects in Xcode 9.)


If App Sandbox is set to ON, many features would not work unless properly enabled.

Thanks OOper.


That did the trick.


Just something else:

If someone knows some helpfull links or tutorials for developing cocoa applications. Please let me know.

I just started with xcode and swift and dont know whats the best way to start with.

One things for sure. I really like this ;P

If you have a completely different question, you'd better start a new thread for it. Something else in a marked-as-solved thread may not get much attention.

Maybe Getting Started would be a better place.


As for me, I had started OS X programming long, long time ago when the first public beta was out, I could not have found any good sites or tutorials online then... I believe you can find many good ones now when you post the question in a proper place.

I will do that OOper.


Thanks for the help.