Saehun Sean Oh Saehun Sean Oh - 6 months ago 43x
Swift Question

Cocoa webview UserAgent "webkit-legacy" issue

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 (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..

Screenshot of testing app

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.

  1. Shared picture does not show up - I labeled as 2 in the picture.

    • again, from other browsers or released apps, it shows the pictures that I shared with participants like below. (of course I censored the pictures)
      example of browser showing shared pics

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...

TLDR: How do I make this work every time I load the view?

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

  • send/receive messages
  • view shared pictures

work just fine.

I'm planning on working on this for

  • download image
  • notification

If you are interested in helping me out, please comment!

Thanks all for your help :)