Search code examples
iosswiftuiwebviewitunes

Present iTunes link inside of a WebView (prevent redirect)?


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

The problem is that these links when loaded in a browser automatically redirect to the iTunes app, rather than showing the content in the UIWebView as I'm attempting to do.

How can I either (1) prevent the redirect so that the content is displayed (2) is there another way to form the iTunes link that will not redirect? (3) any other options?

Update: result using ThunderStruck's code: enter image description here


Solution

  • A possible work-around is to request the desktop-mode of the website, which will display the intended content instead of redirecting you.

    Using UIWebView:

    UserDefaults.standard.register(defaults: ["UserAgent": "Custom-Agent"])
    

    Make sure you register this custom agent before loading the URLRequest. Note that this method would apply for all UIWebView objects in your application. If you want just some specific views to load the desktop version, you'll need to use WKWebView as follows, seeing as it allows you to use a custom agent per object.

    Using WKWebView:

    First, you must import WebKit. Then, initialize it like this:

    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: (Integrating WKWebView)

    Unfortunately, you can't add a WKWebView in IB as of XCode 8, so you'll have to add it programmatically. The good news here is that you can use the frame of the UIWebView you created in IB to slightly ease the programmatic instantiation of the WKWebView object

    Check this out: (untested 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's 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
    }