【问题标题】:Present iTunes link inside of a WebView (prevent redirect)?在 WebView 中显示 iTunes 链接(防止重定向)?
【发布时间】:2017-06-06 19:41:17
【问题描述】:

我想在UIWebView 中显示一个 iTunes 链接,例如https://itunes.apple.com/us/album/burn-that-broken-bed/id1120162623?i=1120163074&uo=4

问题是这些链接在浏览器中加载时会自动重定向到 iTunes 应用程序,而不是像我试图做的那样在 UIWebView 中显示内容。

我怎样才能 (1) 阻止重定向以便显示内容 (2) 是否有另一种方法可以形成不会重定向的 iTunes 链接? (3) 还有其他选择吗?

更新:使用 ThunderStruck 代码的结果:

【问题讨论】:

  • 您想在网络视图中显示它的任何特殊原因?如果不是,您可以始终使用 SKStoreProductViewController 弹出窗口,它将显示实际的应用程序页面和一个按钮来安装它。
  • 我正在展示 webviews 的集合视图,iTunes 链接是我想向用户显示的单元格类型之一,即与 SoundCloud YouTube Spotify 等一起

标签: ios swift uiwebview itunes


【解决方案1】:

一种可能的解决方法是请求网站的桌面模式,这将显示预期的内容而不是重定向您。

使用 UIWebView:

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

确保在加载URLRequest 之前注册此自定义代理。请注意,此方法将适用于您的应用程序中的所有 UIWebView 对象。如果您只想加载一些特定视图来加载桌面版本,则需要使用WKWebView,如下所示,因为它允许您为每个对象使用自定义代理。

使用 WKWebView:

首先,您必须import WebKit。然后,像这样初始化它:

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)

更新:(集成 WKWebView)

不幸的是,从 XCode 8 开始,您无法在 IB 中添加 WKWebView,因此您必须以编程方式添加它。这里的好消息是,您可以使用您在 IB 中创建的 UIWebView 的 frame 来稍微简化 WKWebView 对象的编程实例化

检查一下:(未经测试的代码)

// 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
    }
}

在您的自定义单元格文件中

class MyCustomCell: UICollectionViewCell {
    var wkWebView: WKWebView!    // add this line
    @IBOutlet weak var webView: UIWebView!     // the one you created in IB
}

然后在你的 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
}

【讨论】:

  • 谢谢,这是一个很好的解决方案...不适合我的用例,因为我正在将其他非 iTunes 网址加载到我的收藏视图(例如 Amazon、SoundCloud、Spotify 等)中,但仍然感谢您回答问题。
  • 那么每个collection view cell都有一个UIWebView?如果是这种情况,请尝试将它们更改为 WKWebView,以便您可以为每个单元格指定 customUserAgent 以满足您的需要。如果不是这种情况,请告诉我它是什么,以便我可以尝试更新我的答案以更好地适应您的问题。只是希望它对你公平:-)
  • 谢谢!是的,每个集合视图单元都有一个 UIWebView,我不知道如何将它们更改为 WKWebView,因为它不能从 IB 拖放?换句话说,UIWebView 是界面构建器中可以放入自定义单元格的所有内容。
  • 没问题!我更新了我的答案。如果这不能解决您的问题,请告诉我,我不介意再次更新!干杯:-)
  • 谢谢,虽然您的原始代码在 Playground 中工作以显示应用商店页面,但当我在我的项目中实施您更新的答案时,我仍然得到一个重定向页面,在模拟器中显示此页面...在设备上,当您滚动到您被重定向到 App Store 的单元格时
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-20
  • 2014-12-15
  • 2020-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多