Load desktop version WKWebView iOS 9
To help any who find themselves here for an answer.
The solution was
UserDefaults.standard.register(defaults: ["UserAgent" : "Chrome Safari"])
Request desktop site WKWebview not working in Swift 4
Here's my tested code:
class ViewController: UIViewController {
var webview: WKWebView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
createWebView()
if let url = URL(string: "https://quora.com") {
load(url: url)
}
}
private func createWebView() {
let config = WKWebViewConfiguration()
let webview = WKWebView(frame: self.view.frame, configuration: config)
webview.uiDelegate = self
webview.navigationDelegate = self
self.webview = webview
self.view.addSubview(webview)
}
private func load(url: URL) {
var request = URLRequest(url: url)
let userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"
request.addValue(userAgent, forHTTPHeaderField: "User-Agent")
webview?.load(request)
}
}
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("\(#function)")
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
print("\(#function): \(error)")
}
}
extension ViewController: WKUIDelegate {
}
And the resulting page:
Cheers!
Unable to load desktop site using WKWebView
First of all applicationNameForUserAgent probably isn't what you want since that gets appended to the end of the default user agent string. You are probably looking for WKWebView.customUserAgent which will completely override the default user agent.
For setting desktop mode there is a delegate method in WKNavigationDelegate you can use to specify desktop mode. Introduced in iOS 13. If you don't set the user agent manually with customUserAgent then WKWebView will automatically use a desktop user agent instead of a mobile one.
@available(iOS 13.0, *)
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, preferences: WKWebpagePreferences, decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void) {
preferences.preferredContentMode = .desktop
decisionHandler(.allow, preferences)
}
WKWebView only showing desktop site
I don't have control over the CSS of the page I'm displaying.
It was using the following to calculate breakpoints:
width=device-width, initial-scale=1
@media screen and (min-width: 650px)
Adding this to the template that uses that CSS solved it for me:
<meta name="viewport" content="width={{ $width }}, shrink-to-fit=YES">
Where {{ $width }}
is:
UIScreen.mainScreen().bounds.size.width
desktop browsing in iOS 13 with WKWebView?
I was able to get it to work by using "Version/13.0.1 Safari/605.1.15"
as my applicationNameForUserAgent
string.
(Where did I get that string? Basically I simply stole it from the 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.
WKWEBVIEW to display only mobile version of website
Do you already tried to clear all caches and set user agent?
Something like this:
let dataTypes = NSSet(array: [
WKWebsiteDataTypeDiskCache,
WKWebsiteDataTypeOfflineWebApplicationCache,
WKWebsiteDataTypeMemoryCache,
WKWebsiteDataTypeLocalStorage,
WKWebsiteDataTypeCookies,
WKWebsiteDataTypeSessionStorage,
WKWebsiteDataTypeIndexedDBDatabases,
WKWebsiteDataTypeWebSQLDatabases])
let date = NSDate(timeIntervalSince1970: 0)
WKWebsiteDataStore.defaultDataStore().removeDataOfTypes(websiteDataTypes as! Set<String>, modifiedSince: date, completionHandler:{ })
and after this cleaning, set a user agent
webview.customUserAgent = "Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5"
Maybe you should try this one, but maybe isn't the best solution
How can I request a desktop version of a webpage using UIWebView in Swift 3.0?
A WKWebView should solve your problem - you can define it in your code. Do this:
var webView : WKWebView!
override func loadView() {
super.loadView()
let config = WKWebViewConfiguration()
webView = WKWebView(frame: self.view.frame, configuration: config)
self.webView!.uiDelegate = self
webView.customUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"
//load URL here
let url = NSURL(string: "https://stackoverflow.com/")!
webView.load(URLRequest(url: url as URL))
self.view.addSubview(webView)
}
Related Topics
How to Load My Own Reality Composer Scene into Realitykit
How to Implement iOS 11's Mkusertrackingbutton
Possible to Pass an Enum Type Name as an Argument in Swift
Suppressing Implicit Returns in Swift
Gcd with Static Functions of a Struct
Fblpromises Framework Not Found
How to Declare Swift Implicitly Unwrapped Optional as a Constant
Why Can't I Mutate a Variable Initially Set to a Certain Parameter When the Func Was Called
Why Is 'Throws' Not Type Safe in Swift
How to Use Println in Swift to Format Number
How to Request a Desktop Version of a Webpage Using Uiwebview in Swift 3.0
Swift Can't Infer Generic Type When Generic Type Is Being Passed Through a Parameter
Add Constraints to Generic Parameters in Extension
How to Mimic 'Uitableviewcontroller' Showing of the Large Titles in 'Navigationbar' on iOS 11
How Do Generators Whose Element Is Optional Know When They'Ve Reached the End