3

Im currently working on an app in SwiftUI and im hoping to embed a youtube video and auto play in swiftui. This seems like a difficult task and I haven't found much info on them. I managed to find a WKWebView workaround however it doesnt autplay the video and plays it in full screen,

Is there a way to autoplay a youtube video in swift ui ?

This is the code I got to work so far:

struct WebView: UIViewRepresentable {

    func makeUIView(context: Context) -> WKWebView {
        WKWebView(frame: .zero)
    }

    func updateUIView(_ view: WKWebView, context: UIViewRepresentableContext<WebView>) {

        let request = URLRequest(url: URL(string: "https://www.youtube.com/watch?v=V_r5RCjRdqM")!)

        view.load(request)
        
        
    }
}

struct CardView: View {
    VStack {
                
                WebView()
                 
                
                
                HStack {
                    VStack(alignment: .leading) {
                        Text(category)
                            .font(.headline)
                            .foregroundColor(.secondary)
                        Text(heading)
                            .font(.title)
                            .fontWeight(.black)
                            .foregroundColor(.primary)
                            .lineLimit(3)
                        Text(author.uppercased())
                            .font(.caption)
                            .foregroundColor(.secondary)
                    }
                    .layoutPriority(100)
                    
                    Spacer()
                }
                .padding()
            }
            .cornerRadius(20)
            .overlay(
                RoundedRectangle(cornerRadius: 20)
                    .stroke(Color(.sRGB, red: 150/255, green: 150/255, blue: 150/255, opacity: 0.1), lineWidth: 1)
            )
                .padding([.top, .horizontal])
            .frame(height:UIScreen.main.bounds.height*0.5)
        }
    }
}
Kharl Mccatty
  • 85
  • 1
  • 10
  • I found a similar question that has been answered for your case. [See here](https://stackoverflow.com/questions/55626173/how-to-autoplay-youtube-video-in-wkwebview) – Phạm Đông Aug 19 '20 at 02:08
  • @PhạmĐông This is not the same question as mine is asking about swift ui and they are asking about traditional swif – Kharl Mccatty Aug 19 '20 at 14:03

0 Answers0