Reputation: 1213
I have a NSViewController
which displays various WKWebView
at specific locations inside a NSView. If the frame of the WKWebView
gets small - it switches to the mobile version of the website. Is there a way to always show the full (desktop version) website in the WKWebView
?
My first idea was to inject some javascript and set the viewport to a specific size to force the website to display a "Desktop" view.
let viewportScriptString = "var meta=document.createElement('meta');meta.name=\"viewport\";meta.content=\"width=1920\";document.getElementsByTagName('head')[0].appendChild(meta);"
let viewportScript = WKUserScript(source: viewportScriptString, injectionTime: .AtDocumentStart, forMainFrameOnly: true)
let controller = WKUserContentController()
controller.addUserScript(viewportScript)
let config = WKWebViewConfiguration()
config.userContentController = controller
let nativeWebView = WKWebView(frame: CGRect.zero, configuration: config)
Unfortunately this does not work and the website is still scaled. To demonstrate the behaviour i try to achieve please see the following screenshot.
==EDIT==
thanks for pointing me to some similar questions. I tried the following the solutions that worked for other users. Unfortunately I had no luck. Maybe WKWebView
is different on macOS - the other questions are about iOS.
NSUserDefaults
CustomUserAgent
on WKWebView
ApplicationNameForUserAgent
on WKWebViewConfiguration
User-Agent
to NSUrlRequest
Upvotes: 2
Views: 1471
Reputation: 1385
You should change the user agent, for example:
UserDefaults.standard.register(defaults: ["UserAgent" : "Chrome Safari"])
Upvotes: 3