Cocoa webview UserAgent“webkit-legacy”问题 [英] Cocoa webview UserAgent "webkit-legacy" issue

查看:353
本文介绍了Cocoa webview UserAgent“webkit-legacy”问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我解决了这个问题。看下面的选择的答案!


我一直在努力找到原因OSX Cocoa应用程序的WebView的行为不同于Safari 。事实证明,用户代理是不同的(显然?),我访问的网站不知道如何处理。



令人惊讶的是, a href =https://messenger.com =nofollow noreferrer> https://messenger.com (Facebook chat)。



问题是它不会在屏幕上显示图片。它加载,但它实际上不显示。看看这个..





如果你看看我编号为'2'的区域,你只是看到空的空间。我没有检查出来。



这是我的原始问题链接:


为了调试这个,我打开了Inspect Element,我发现了这一点。

 < body class =webkit-legacy webkit mac x1 body_textalign Locale_en_US _z4_dir =ltr> 

当我从Safari载入完全相同的网页时,我会看到:

 < body class =safari webkit mac x1 body_textalign Locale_en_US _z4_dir =ltr> 

所以我决定从我的应用程序的WebView替换那行,有用!因此...



TLDR:如何在每次加载视图时都进行此操作?



试图找到一些方法来设置我的用户代理到Safari,但我不能得到这个工作。有任何建议吗?

解决方案

如所承诺,这是答案。



我在 ViewController 类中添加了此函数。

  func webView发送者:WebView !, didFinishLoadForFrame框架:WebFrame!){
webView.stringByEvaluatingJavaScriptFromString(document.body.className ='safari webkit mac x1 Locale_en_US _z4_';)
}



很简单,但是工作得很好。



应用程序本身的问题(例如,下载图像按钮不会触发文件浏览器对话框),但基本功能如




  • 发送/ / li>
  • 查看分享的图片



工作得很好。



我计划为




  • 下载图片



如果您有兴趣帮助我,请评论!



感谢您的帮助:)


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.

  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)

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 :)

这篇关于Cocoa webview UserAgent“webkit-legacy”问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆