A possible workaround is to request an operating mode on a website that displays the intended content, rather than redirecting you.
Using UIWebView:
UserDefaults.standard.register(defaults: ["UserAgent": "Custom-Agent"])
Before downloading URLRequest
make sure you register this custom agent. Please note that this method will be applied to all UIWebView
objects in your application. If you want only certain views to display the desktop version loading, you need to use WKWebView
as follows, as this allows you to use a custom agent for each object.
Using WKWebView:
First, you must import WebKit
. Then initialize it as follows:
let url = URL(string: "https://itunes.apple.com/us/album/burn-that-broken-bed/id1120162623?i=1120163074&uo=4")! let wkWebView = WKWebView(frame: self.view.frame, configuration: WKWebViewConfiguration()) wkWebView.uiDelegate = self
Update: (WKWebView Integration)
Unfortunately, you cannot add WKWebView to IB with Xcode 8, so you have to add it programmatically. The good news is that you can use the UIWebView frame
created in IB to make the programmatic copy of the WKWebView object a bit easier.
Check this out: (unverified code)
// for ease of use extension WKWebView { func setDesktopMode(on: Bool) { if on { 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" return } customUserAgent = nil } }
And in your custom cell file
class MyCustomCell: UICollectionViewCell { var wkWebView: WKWebView!
Then in your UIViewController
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellIdentifier", for: indexPath) as! MyCustomCell let url = URL(string: "url here")! cell.wkWebView = WKWebView(frame: cell.webView.frame, configuration: WKWebViewConfiguration())