How to play Vimeo videos in iOS Swift?

Caspert picture Caspert · Nov 23, 2016 · Viewed 11.1k times · Source

I create an iOS app that make use of Vimeo for playing videos. I was wondering what the best method is to show Vimeo videos within iOS Swift.

I have fixed that, when an image is clicked, the placeholder image will be hidden, but the video won't play directly. Beside that all Vimeo Interface elements are visible. I know you should have to give credits to Vimeo, so it goes without saying that I display an Vimeo logo at the bottom of the video. Is there a possibility to hide all Vimeo elements of the web player?

Sources where I can find more information would be nice. If there are any questions left, let me know! Thanks in advance.

Answer

Nitin Nain picture Nitin Nain · Nov 23, 2016

The best way that I've been able to figure out is to use a WKWebView or UIWebView. Steps:

  1. Add a WKWebView of desired size inside the View Controller.

  2. Get the video embed code from the original Vimeo video (Click on the "Share" button below the video to find the embed code.)

    enter image description here

  3. Click on "More Options" on the Vimeo Share sheet that pops-up to configure the look of the embed video.

  4. Use the following code sample to embed the video:

    let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
    self.view.addSubview(webView)
    
    let embedHTML="<html><head><style type=\"text/css\">body {background-color: transparent;color: black;}</style><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=yes\"/></head><body style=\"margin:0\"><div><iframe src=\"//player.vimeo.com/video/139785390?autoplay=1&amp;title=1&amp;byline=1&amp;portrait=0\" width=\"640\" height=\"360\" frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div></body></html>"
    let url = URL(string: "https://")!
    webView.loadHTMLString(embedHTML as String, baseURL:url )
    webView.contentMode = UIViewContentMode.scaleAspectFit