最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift - WKWebView not loading YouTube videos in iOS app - Stack Overflow

programmeradmin4浏览0评论

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
  • In update you need to be careful to only load if not already loaded – malhal Commented Feb 17 at 15:09
  • It may depend on the videoID you want to display. Using your code with videoID = "M7lc1UVf-VE" the video is displayed and works well for me. But when I use videoID = "09839DpTctU" (a music video), it displays "Video is unavailable". Maybe you have to pay money for these. However, using let embedURLString = "https://www.youtube/watch?v=\(videoID)&autoplay=1&playsinline=1" all works. – workingdog support Ukraine Commented Feb 18 at 2:23
Add a comment  | 

1 Answer 1

Reset to default 0

You must allow arbitrary loads over HTTP/HTTPS, enabling YouTube video playback.

  1. Open (or create) Info.plist file
  2. Add NSAppTransportSecurity key of type Dictionary (sets automatically)
  3. Add NSAllowsArbitraryLoads of type Boolean (it sets to String by default, be sure to check!) and its value to YES

Now it should work

发布评论

评论列表(0)

  1. 暂无评论