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
Force UIWebView to load mobile version of website, like Safari does
I forgot to set the constraints for the UIWebView
, which caused the content to be rendered wrongly. I don't know how that's relevant, but constraining the UIWebView
seems to not only render the content correctly but also display the mobile site.
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!
Force SFSafariViewController to load the desktop version of a website
As of iOS 9, sadly no. Your best bet might be to create your own web view and use a different user agent as @vizllx suggests in the comments.
Related Topics
Using Shader Modifiers to Animate Texture in Scenekit Leads to Jittery Textures Over Time
How to Animate a Nslayoutconstraint in Swift
iOS Where to Put Custom Cell Design? Awakefromnib or Cellforrowatindexpath
How to Go to Specific Native View Controller from React-Native Code
Tabbarcontroller Adding Custom Button Not Clickable Issue
How to Fix Crash When Tap to Select Row After Scrolling The Tableview
Mpvolumeview Does Not Show Route Button on Launch
Add a UIbutton as a Subview to a UItabbar
iOS How to Get a List of Already Purchased Products
Responseserializer 'Cannot Call Value of Non-Function Type 'Nshttpurlresponse'' with Swift 3
Can't Use @Observedobject on Real iPhone
Pass a Nsdictionary as Parameter to UItapgesturerecognizer
How to Request Photo Gallery Permissions in iOS a Second Time
Applying Different Attributes for Different Portions of an Nsattributedstring
Uicollectionview Inside Tableviewcell Not Called
iOS Wkwebview Swift JavaScript Enable