Created
August 7, 2018 06:45
-
-
Save oaleeapp/6e9825de967f12d02b0de37feebd1a1b to your computer and use it in GitHub Desktop.
Embed Youtube video into UIWebView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// let videoLink = "https://www.youtube.com/watch?v=[your-video-id]" | |
if let videoId = URLComponents(string: videoLink)?.queryItems?.first(where: { $0.name == "v" })?.value { | |
let videoWebView = UIWebView(frame: self.videoContainerView.bounds) | |
videoWebView.allowsInlineMediaPlayback = true | |
videoWebView.autoresizesSubviews = true | |
videoWebView.scrollView.bounces = false | |
self.videoContainerView.addSubview(videoWebView) | |
let width: CGFloat = videoWebView.frame.size.width | |
let height: CGFloat = videoWebView.frame.size.height | |
let URL = String(format: "<html><body style='margin:0px;padding:0px;'><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1\"><iframe style='margin:0px;padding:0px;' width=\"%f\" height=\"%f\" src=\"https://www.youtube.com/embed/\(videoId)?enablejsapi=1&showinfo=0&rel=0&controls=1&playsinline=1&autoplay=0\" frameborder=\"0\"></iframe></body></html>",width,height) | |
videoWebView.loadHTMLString(URL, baseURL: nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment