Home > Mobile >  xamarin WKWebView, ipad does not support youtube full screen
xamarin WKWebView, ipad does not support youtube full screen

Time:01-03

I implemented a webview using the WkWebview renderer.

Fullscreen works fine on ipone. But it doesn't work on iPad.

An image like the one below will appear.

enter image description here

I saw an article saying to do webViewConfiguration.allowsInlineMediaPlayback = true and applied it but it didn't work. What's the problem?

Here is my code.

   public class MyWebViewRenderer : WkWebViewRenderer, IWKScriptMessageHandler, IWKNavigationDelegate
   {

    WKUserContentController userController;

        public MyWebViewRenderer() : this(new WKWebViewConfiguration())
        {
            
        }

        public MyWebViewRenderer(WKWebViewConfiguration config) : base(config)
        {

            userController = config.UserContentController;
            var script = new WKUserScript(new NSString(_JavascriptFunction_CSharpOpenWeb), WKUserScriptInjectionTime.AtDocumentEnd, false);

            userController.AddUserScript(script);
            
            userController.AddScriptMessageHandler(this, "invokeAction_CSharpOpenWeb");
        }

protected async override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

          

            if (e.OldElement != null)
            {
                userController.RemoveAllUserScripts();
                userController.RemoveScriptMessageHandler("invokeAction_CSharpOpenWeb");
                MyWebView myWebView = e.OldElement as MyWebView;
                myWebView.Cleanup();
            }

            if (e.NewElement != null)
            {
                this.NavigationDelegate = new MyNavigationDelegate(this);
               
                var webView = (MyWebView)Element;
                 
                webView.UriChanged  = async (s1, e1) =>
                {
                    
                        NSUrl nsurl = new NSUrl(webView.Uri);
                        Configuration.AllowsInlineMediaPlayback = true;
                        NSMutableUrlRequest request = new NSMutableUrlRequest(nsurl);
                       
                        await SetCookies();
                      
                        LoadRequest(request);
                };

                if (!string.IsNullOrEmpty(webView.Uri))
                {
                   
                        NSUrl nsurl = new NSUrl(webView.Uri);
                        NSMutableUrlRequest request = new NSMutableUrlRequest(nsurl);
                   

                        await SetCookies();

                        LoadRequest(request);
                }
            }
        }
}

Can you tell me what I did wrong?

CodePudding user response:

  1. Configuration.AllowsInlineMediaPlayback = true; is the OPPOSITE of what you want.

Set it to false:

Configuration.AllowsInlineMediaPlayback = false;

  1. If that doesn't fix it, then you may need to do that earlier in the code.

See if it works when done in the constructors:

public MyWebViewRenderer() : this(new WKWebViewConfiguration())
{
    EnsureFullScreen();
    ...
}
    
public MyWebViewRenderer(WKWebViewConfiguration config) : base(config)
{
    EnsureFullScreen();
    ...
}


private void EnsureFullScreen()
{
    if (Control.Configuration.AllowsInlineMediaPlayback)
        Configuration.AllowsInlineMediaPlayback = false;
}
  • Related