0

I'm developing an iOS App in SwiftUI that has a link to a YouTube video. The video on YT is a Private one and I don't want anyone to be able to find the direct link, problem is, the YT controls show up on the video so you can easily just press and open the link up in Safari and therefore get the direct link.

Is there anyway in SwiftUI to prevent that so it looks like the video is just a locally resourced one please?

I've create a Swift View so I can parse other videos into it:

import SwiftUI
import WebKit

struct VideoView: UIViewRepresentable {
    
    let videoID: String
    
    func makeUIView(context: Context) -> WKWebView {
        return WKWebView()
    }
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
        guard let youtubeURL = URL(string: "https://www.youtube.com/embed/\(videoID)") else {return}
        uiView.scrollView.isScrollEnabled = false
        uiView.load(URLRequest(url: youtubeURL))
    }
}

And then just have this in the basic ContentView with AVKit imported:

Text("YouTube Video")
    .padding()
VideoView(videoID: "U8Cd_McCdow")
    .frame(minHeight: 0, maxHeight: UIScreen.main.bounds.height * 0.3)
    .cornerRadius(12)
    .padding(.horizontal, 24)
James Hunt
  • 31
  • 6
  • 2
    This isn’t a SwiftUI issue — there isn’t a SwiftUI YouTube view/control – jnpdx Jun 04 '22 at 15:54
  • Could you please display the code you're using? Also, you could load the video in using `AVKit` and a URL - that is more customizable. – Mr Developer Jun 04 '22 at 17:10
  • @jnpdx I've never worked with Vimeo but might give that a try to see if their controls are kept private or not. – James Hunt Jun 04 '22 at 17:52
  • @MrDeveloper Just updated my question with the code. – James Hunt Jun 04 '22 at 17:56
  • You'll likely need to inject javascript into the WKWebView to do this. However, the methods of doing it look like they change over time. See https://stackoverflow.com/questions/61811254/hide-youtube-video-controls-title-watch-later-share-using-html-css-javas and https://stackoverflow.com/questions/20710764/can-you-hide-the-controls-of-a-youtube-embed-without-enabling-autoplay – jnpdx Jun 04 '22 at 17:57
  • Take a look at this - if you are OK with using `AVKit`, you can play the video from a remote URL. https://www.hackingwithswift.com/quick-start/swiftui/how-to-play-movies-with-videoplayer – Mr Developer Jun 04 '22 at 18:52

0 Answers0