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)
}
}
}