Submit iTunes link inside WebView (prevent redirection)?

I want to display the iTunes link in a UIWebView for example. https://itunes.apple.com/us/album/burn-that-broken-bed/id1120162623?i=1120163074&uo=4

The problem is that these links when loading in the browser are automatically redirected to the iTunes application, and the contents are not displayed in the UIWebView , as I try to do.

How can I (1) prevent redirection so that content is displayed (2), is there any other way to create an iTunes link that will not be redirected? (3) any other options?

Update: result using ThunderStruck code: enter image description here

+6
source share
1 answer

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 // Optional line - must conform to WKUIDelegate // the line below specifies the custom agent, which allows you to request the desktop version of the website wkWebView.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" wkWebView.load(URLRequest(url: url)) self.view.addSubview(wkWebView) 

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! // add this line @IBOutlet weak var webView: UIWebView! // the one you created in IB } 

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()) // using the webView frame that was created in IB cell.wkWebView.uiDelegate = self // Optional line - must conform to WKUIDelegate cell.wkWebView.setDesktopMode(on: true) // true = loads desktop mode (for your iTunes URLs) cell.wkWebView.load(URLRequest(url: url)) return cell } 
+3
source

Source: https://habr.com/ru/post/1014296/


All Articles