简体   繁体   English

使用 WKWebView 在 iOS 13 中进行桌面浏览?

[英]desktop browsing in iOS 13 with WKWebView?

According to WWDC 2019 video 203, I should be able to see desktop browsing in a full-screen WKWebView on an iPad.根据 WWDC 2019 视频 203,我应该能够在 iPad 上的全屏 WKWebView 中看到桌面浏览。 But I'm not seeing it.但我没有看到它。 For example, when I go to google in my WKWebView, I'm clearly seeing the mobile version of the site.例如,当我在 WKWebView 中搜索 go 时,我清楚地看到了该网站的移动版本。

I've tried everything the video suggests.我已经尝试了视频建议的所有内容。 I've set the applicationNameForUserAgent , and the web inspector shows that it is being set as they advise in the video:我已经设置了applicationNameForUserAgent ,并且 web 检查器显示它正在按照视频中的建议进行设置:

"5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/1.0 MyBrowser/1.0"

I've also implemented the navigation delegate to request .desktop mode:我还实现了导航委托以请求.desktop模式:

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, preferences: WKWebpagePreferences, decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void) {
    preferences.preferredContentMode = .desktop
    print("asking for desktop version")
    decisionHandler(.allow, preferences)
}

That method is being called.正在调用该方法。 But I'm still seeing the mobile version of the site.但我仍然看到该网站的移动版本。

When I go to the same site in Safari on the same iPad, or if I use an SFSafariViewController, I do see the desktop version of the site, as advertised.当我 go 到 Safari 上的同一站点 iPad,或者如果我使用 SFSafari 作为广告视图控制器时,我确实看到了该站点的桌面版本。 How can I get that version of the site in my WKWebView?如何在我的 WKWebView 中获取该版本的网站?

I was able to get it to work by using "Version/13.0.1 Safari/605.1.15" as my applicationNameForUserAgent string.通过使用"Version/13.0.1 Safari/605.1.15"作为我的applicationNameForUserAgent字符串,我能够让它工作。

(Where did I get that string? Basically I simply stole it from the SFSafariViewController.) (我从哪里得到那个字符串?基本上我只是从 SFSafariViewController 偷来的。)

This seems like a bug.这似乎是一个错误。 Apple's video implies that I should not have to lie about who the user agent is in order to get desktop browsing. Apple 的视频暗示我应该为了获得桌面浏览而对用户代理是谁撒谎。

I'm disappointed at this bug of WKWebView on iPadOS (iPad iOS13).我对 iPadOS (iPad iOS13) 上 WKWebView 的这个错误感到失望。

This is the code that get each version programmatically, generates a string in the format indicated by @matt, and adds it to user agent.这是以编程方式获取每个版本、以@matt 指示的格式生成字符串并将其添加到用户代理的代码。

let systemVersion = UIDevice.current.systemVersion
let webkitVersion = (Bundle(for: SFSafariViewController.self).object(forInfoDictionaryKey: "CFBundleVersion") as? String) ?? "605.1.5"
webConfig.applicationNameForUserAgent = "Version/\(systemVersion) Safari/\(webkitVersion)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM