【问题标题】:Embedded YouTube video in a UIWebView fails to playUIWebView 中嵌入的 YouTube 视频无法播放
【发布时间】:2026-01-17 15:00:01
【问题描述】:

我想用custom Cell 实现UITableView,它使用UIWebView,但我的问题是UIWebView 不显示视频。上完课后我想复习https://w%20ww.youtube.com/watch?v=rcVv1N1hReQ,但这是不可能的。

【问题讨论】:

  • 你的问题是什么?

标签: html ios swift uiwebview


【解决方案1】:

代码

var testArray = ["https://www.youtube.com/embed/W7qWa52k-nE", "https://www.youtube.com/embed/vH7i-qpoFVk", "https://www.youtube.com/embed/V_GTrL-UVmE"]

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return testArray.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! WebViewTableViewCell

        cell.videoWeb.allowsInlineMediaPlayback = true

        let embededHTML = "<html><body><iframe src=\"\(testArray[indexPath.row])?playsinline=1\" width=\"'\(cell.videoWeb.frame.width)'\" height=\"'\(cell.videoWeb.frame.height)'\" frameborder=\"10\" allowfullscreen></iframe></body></html>"

        cell.videoWeb.loadHTMLString(embededHTML, baseURL: NSBundle.mainBundle().bundleURL)
        cell.videoWeb.scrollView.scrollEnabled = false

        return cell
    }

截图

测试项目

test project link

【讨论】: