I solved this. Look at the selected answer below!
I've been fighting to find what causes the WebView of OSX Cocoa application to act differently from Safari. It turns out that the user-agent is different (sort of obvious?) and the website I'm visiting does not know how to handle that.
Surprisingly, it's https://messenger.com (facebook chat).
The problem is that it doesn't display the picture on the screen. It does load, but it doesn't actually display. Take a look at this..
If you look at the area that I numbered as '2' you just see empty space. I didn't censor that out. It's just empty.
So here's my original question link: Simple Swift Cocoa app with WebKit: Upload picture doesn't work
I solved the first issue (thanks to the answers :D), but second issue persists.
To debug this, I opened the Inspect Element and I found this out.
<body class=" webkit-legacy webkit mac x1 body_textalign Locale_en_US _z4_" dir="ltr">
When I did load the exact same page from Safari, I'd see this:
<body class="safari webkit mac x1 body_textalign Locale_en_US _z4_" dir="ltr">
So I decided to replace that line from WebView of my app, and viola! it works! so...
I tried to find some methods to set up my user agent to Safari, but I can't get this to work. Any suggestion please?
As promised, here's the answer.
I added this function in the ViewController
class.
func webView(sender: WebView!, didFinishLoadForFrame frame: WebFrame!) {
webView.stringByEvaluatingJavaScriptFromString("document.body.className = 'safari webkit mac x1 Locale_en_US _z4_';")
}
Pretty simple, but it's working perfectly fine.
I still have some problems with the app itself (e.g. Download image button doesn't trigger file explorer dialog), but basic functions like
work just fine.
I'm planning on working on this for
If you are interested in helping me out, please comment!
Thanks all for your help :)