3

Below is the code that I currently have. How do I enable inline media playback for swiftUI? I want the video to automatically play when the webview is appeared. Below is the code I have so far. Thanks in advance.

Button(action: {
                    self.isPlayed.toggle()//toggles the boolean isPlayed
                    self.appear.toggle()//toggles the boolean appear


                }, label: {

                    //if the boolean of isPlayed and
                    if isPlayed && appear{
                        Image(systemName: "pause")
                            .font(Font.system(size: 30))

                    } else{
                        Image(systemName:"play")
                            .font(Font.system(size: 30))
                    }

                })
                Spacer()
                }

            if appear == true{

               WebView(request: URLRequest(url: URL(string: "http:www.\(websites[counter])?playsinline=1")!))
                   .frame(width:300, height:300)
                .padding()
                .opacity(1)

            }

        }

        }
}

struct WebView: UIViewRepresentable {
    let request: URLRequest//pass the website to webkit

    func makeUIView(context: Context) -> WKWebView {
        return WKWebView()

    }
    func updateUIView(_ uiView: WKWebView, context: Context) {
        uiView.load(request)

    }
}
Thomas Kim
  • 33
  • 1
  • 5

2 Answers2

6

It can be done via WKWebViewConfiguration as below

func makeUIView(context: Context) -> WKWebView {
    let configuration = WKWebViewConfiguration()
    configuration.allowsInlineMediaPlayback = true

    let webView = WKWebView(frame: .zero, configuration: configuration)
    return webView
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Thanks it worked! However the audio seems to auto mute... but I've also heard that this has to do with the browser? – Thomas Kim Jan 21 '20 at 05:54
  • There is known workaround, please read [How to force WKWebView to ignore hardware silent switch on iOS?](https://stackoverflow.com/questions/56460362/how-to-force-wkwebview-to-ignore-hardware-silent-switch-on-ios) – Asperi Jan 21 '20 at 06:01
  • Thank you, and apologies for my lack of knowledge but where am I to implement @obj functions and override function when I have used struct and not classes? – Thomas Kim Jan 21 '20 at 07:29
  • Update: for @objc func willResignActive() { disableIgnoreSilentSwitch(webView) } I have an error when passing wkWebView paramater. The error says Cannot convert value of type '(WKWebView, WKNavigation?) -> ()' to expected argument type 'WKWebView' – Thomas Kim Jan 22 '20 at 06:04
2

I struggled to find a solution, and @Asperi's answer worked for me.

For the record, I was setting the configuration directly to the webView, like this, and it did not work:

webView.configuration.allowsInlineMediaPlayback = true

declaring the webView with the configuration object solved the issue, like this:

let configuration = WKWebViewConfiguration()
configuration.allowsInlineMediaPlayback = true
webView = WKWebView(frame: .zero, configuration: configuration)
gabgren
  • 51
  • 4