arbi.tularov arbi.tularov - 7 months ago 34
Swift Question

Embedded YouTube video in a UIWebView fails to play

I wanted to implement a

UITableView
with
custom Cell
which uses a
UIWebView
, but my problem is that UIWebView does not show video. I tried to repeat after a lesson https://w%20ww.youtube.com/watch?v=rcVv1N1hReQ, but it is impossible.

Answer

Code

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
    }

Screenshot

enter image description here

Test Project

test project link