0

Hello I a have some code that loads a web video into a swift ui view and allows it to be played. Currently the video auto plays when it loads in and I would like to stop this behavior. Additionally the video can only be viewed in full screen mode and I can't figure out how to make it so that it can play while not being in full screen. I also get the warning "Error acquiring assertion: <Error Domain=RBSServiceErrorDomain Code=1 "(originator doesn't have entitlement com.apple.runningboard.assertions.webkit AND originator doesn't have entitlement" I tried the line "webView.configuration.allowsInlineMediaPlayback = true" to allow the video to play while not being full sized and it did not work. If anyone knows how to get these 2 pieces of functionality working I would appreciate it, thanks.

import SwiftUI
import WebKit

struct YouTubeView: UIViewRepresentable {
    let videoId: String
    func makeUIView(context: Context) -> WKWebView {
          let webView = WKWebView()
          webView.configuration.allowsInlineMediaPlayback = true
            
          webView.configuration.mediaTypesRequiringUserActionForPlayback = []
          
          return webView
    }
    func updateUIView(_ uiView: WKWebView, context: Context) {
        guard let demoURL = URL(string: "https://s1.fwmrm.net/m/1/169843/20/89977492/AIDP7266000H_ENT_MEZZ_HULU_8166176_578.mp4") else { return }
        uiView.scrollView.isScrollEnabled = false
        uiView.load(URLRequest(url: demoURL))
    }
}

struct VideoPlayerView: View {
    var ids = "hzls6ZUHCYM"
    var body: some View {
        ZStack {
            ScrollView(showsIndicators: false) {
                VStack {
                    YouTubeView(videoId: ids)
                        .frame(width: 300, height: 175)
                }
            }
        }
    }
}


//modifier correct YouTube struct
struct YouTubeView: UIViewRepresentable {
    var videoID: String
    
    func makeUIView(context: Context) -> WKWebView {
        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.allowsInlineMediaPlayback = true
        return WKWebView(frame: .zero, configuration: webConfiguration)
    }
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
        let embedHTML = """
            <!DOCTYPE html>
            <html>
                <body>
                    <iframe width="800" height="550" src="https://www.youtube.com/embed/\(videoID)" frameborder="0" allowfullscreen></iframe>
                </body>
            </html>
        """
        uiView.loadHTMLString(embedHTML, baseURL: nil)
    }
}
ahmed
  • 341
  • 2
  • 9

1 Answers1

2

try loading your video embedded within html rather than directly to an mp4 url. set the playsinline parameter for the video tag. also set allowsInlineMediaPlayback prior to instantiating the WKWebView object.

as for the entitlement error, other SO answers seem to indicate that this can simply be ignored.

let html = """
    <video
    src="https://s1.fwmrm.net/m/1/169843/20/89977492/AIDP7266000H_ENT_MEZZ_HULU_8166176_578.mp4"
    width="640" height="480"
    controls
    playsinline="true">
"""

struct YouTubeView: UIViewRepresentable {
    func makeUIView(context: Context) -> WKWebView {
        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.allowsInlineMediaPlayback = true //set up config first
        return WKWebView(frame: .zero, configuration: webConfiguration)
    }
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
        uiView.loadHTMLString(html, baseURL: nil) //load video embedded inside html
    }
}
Fault
  • 1,115
  • 1
  • 7
  • 11
  • one question, when I pass in a YouTube videos url into the html, the video Is not able to play and displays a dash through the unpause button. However with my original code It does, what can I do to make a YouTube video play with your modifications? – ahmed May 27 '23 at 20:11
  • nvm I figured it out, all I had to do was simply change the html and use Iframe, I will update my code above for anyone interested to see. – ahmed May 27 '23 at 20:16
  • Fault maybe you can help with this question -> https://stackoverflow.com/questions/76360300/remove-white-background-border-from-swift-video-rendered-using-embedded-html-and – ahmed May 30 '23 at 00:27
  • can you please look at this question as well: https://stackoverflow.com/questions/76624877/swift-long-press-gesture-only-works-on-link-and-not-on-text-portion – ahmed Jul 06 '23 at 02:55