I am developing an iOS app in which I want to show YouTube videos. This is my code
struct YouTubeView: UIViewRepresentable {
let videoID: String
func makeUIView(context: Context) -> WKWebView {
let configuration = WKWebViewConfiguration()
configuration.allowsInlineMediaPlayback = true
if #available(iOS 10.0, *) {
// Ensure that media plays without requiring a user gesture.
configuration.mediaTypesRequiringUserActionForPlayback = []
} else {
configuration.requiresUserActionForMediaPlayback = false
}
let webView = WKWebView(frame: .zero, configuration: configuration)
webView.scrollView.isScrollEnabled = false
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
let embedURLString = "/\(videoID)?playsinline=1"
if let url = URL(string: embedURLString) {
let request = URLRequest(url: url)
uiView.load(request)
}
}
}
struct VideoPlayerView: View {
@EnvironmentObject var viewModel: AppViewModel
var video: Video
@Environment(\.presentationMode) var presentationMode
var body: some View {
VStack {
YouTubeView(videoID: video.youtubeID)
.frame(height: 200)
if !video.watched {
Button("Mark as Watched") {
viewModel.markVideoAsWatched(video: video)
presentationMode.wrappedValue.dismiss()
}
.padding()
} else {
Text("Video already watched")
.padding()
}
}
.padding()
}
}
I checked and the video can be embedded also, so that isn't the issue. But when I launch my app and click on the video, the app just shows a blank white screen.
I am developing an iOS app in which I want to show YouTube videos. This is my code
struct YouTubeView: UIViewRepresentable {
let videoID: String
func makeUIView(context: Context) -> WKWebView {
let configuration = WKWebViewConfiguration()
configuration.allowsInlineMediaPlayback = true
if #available(iOS 10.0, *) {
// Ensure that media plays without requiring a user gesture.
configuration.mediaTypesRequiringUserActionForPlayback = []
} else {
configuration.requiresUserActionForMediaPlayback = false
}
let webView = WKWebView(frame: .zero, configuration: configuration)
webView.scrollView.isScrollEnabled = false
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
let embedURLString = "https://www.youtube/embed/\(videoID)?playsinline=1"
if let url = URL(string: embedURLString) {
let request = URLRequest(url: url)
uiView.load(request)
}
}
}
struct VideoPlayerView: View {
@EnvironmentObject var viewModel: AppViewModel
var video: Video
@Environment(\.presentationMode) var presentationMode
var body: some View {
VStack {
YouTubeView(videoID: video.youtubeID)
.frame(height: 200)
if !video.watched {
Button("Mark as Watched") {
viewModel.markVideoAsWatched(video: video)
presentationMode.wrappedValue.dismiss()
}
.padding()
} else {
Text("Video already watched")
.padding()
}
}
.padding()
}
}
I checked and the video can be embedded also, so that isn't the issue. But when I launch my app and click on the video, the app just shows a blank white screen.
Share Improve this question edited Feb 17 at 15:23 HangarRash 15.1k5 gold badges19 silver badges55 bronze badges asked Feb 17 at 14:30 BhavyabhatiaBhavyabhatia 571 silver badge7 bronze badges 2 |1 Answer
Reset to default 0You must allow arbitrary loads over HTTP/HTTPS, enabling YouTube video playback.
- Open (or create)
Info.plist
file - Add
NSAppTransportSecurity
key of type Dictionary (sets automatically) - Add
NSAllowsArbitraryLoads
of type Boolean (it sets to String by default, be sure to check!) and its value toYES
Now it should work
videoID
you want to display. Using your code withvideoID = "M7lc1UVf-VE"
the video is displayed and works well for me. But when I usevideoID = "09839DpTctU"
(a music video), it displays "Video is unavailable". Maybe you have to pay money for these. However, usinglet embedURLString = "https://www.youtube/watch?v=\(videoID)&autoplay=1&playsinline=1"
all works. – workingdog support Ukraine Commented Feb 18 at 2:23