Forum Xamarin Xamarin.iOS

Ios wkwebview does not display videos attribute

I have a wkwebview based iOS application and in this application users need to see videos. In het web project it all works fine, User see videos and can run the videos.

However in xamarin Ios the user see this:

And cannot run the videos.

In the project this is an video attribute.

How can i solve this so iOS user can also see the videos?

Answers

  • RezaMohamedRezaMohamed USMember ✭✭✭
    edited April 8

    Heres my renderer, works fine

    [assembly: ExportRenderer(typeof(HybridWebView), typeof(WebViewRender))]
    namespace XFForumSample.iOS
    {
        public class WebViewRender : ViewRenderer<HybridWebView, WKWebView>, IWKScriptMessageHandler
        {
            WKUserContentController userController;
    
            public void DidReceiveScriptMessage(WKUserContentController userContentController, WKScriptMessage message)
            {
    
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView> e)
            {
                base.OnElementChanged(e);
    
                this.BackgroundColor = UIColor.Clear;
    
                if (Control == null)
                {
                    userController = new WKUserContentController();
                    var config = new WKWebViewConfiguration() { UserContentController = userController };
                    config.AllowsInlineMediaPlayback = true;
                    var webView = new WKWebView(Frame, config);
                    webView.ScrollView.ScrollEnabled = false;
                    SetNativeControl(webView);
                }
    
                /*if (e.NewElement != null)
                {
                    Control.LoadRequest(new NSUrlRequest(new NSUrl(Element.Uri)));
                }*/
            }
            protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                base.OnElementPropertyChanged(sender, e);
                var hybrid = (HybridWebView)sender;
    
                if (e.PropertyName == HybridWebView.UriProperty.PropertyName && (hybrid != null && !string.IsNullOrEmpty(hybrid.Uri)))
                {
                    Control.LoadRequest(new NSUrlRequest(new NSUrl(hybrid.Uri)));
                }
            }
        }
    }
    

    Also check out this page https://devblogs.microsoft.com/xamarin/uiwebview-deprecation-xamarin-forms/

  • Xamarin_User20Xamarin_User20 Member ✭✭

    Hi Reza,

    Unfortunately this has not solved my problem. I still do not get to see a video when on iOS devices.

    I have implemented you're code like this(Removed certain information on purpase):

        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
    
    
            base.OnElementChanged(e);
    
    
            this.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
    
    
            if (Control == null)
            {
                userController = new WKUserContentController();
                var config = new WKWebViewConfiguration() { UserContentController = userController };
                config.AllowsInlineMediaPlayback = true;
                var webView = new WKWebView(Frame, config);
                webView.ScrollView.ScrollEnabled = false;
                SetNativeControl(webView);
            }
    
        }
    
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
    
            Control.LoadRequest(new NSUrlRequest(new NSUrl(url)));
    
        }
    

    Do you have any idea how I can solve my problem?

  • LandLuLandLu Member, Xamarin Team Xamurai

    Can you share the video link here?
    If you assign a normal youtube link to the webview's source, can it be played successfully?

  • Xamarin_User20Xamarin_User20 Member ✭✭

    The video is an .mp4 file and is located in an folder in my project.

    I cannot publish an video here, but the common test .mp4 wich you can find on w3school also does not work for me.

  • LandLuLandLu Member, Xamarin Team Xamurai

    How did you play a local video using webview?
    Could you please post a basic sample here to help me reproduce this issue?
    You could remove the sensitive video and only leave the code.

  • Xamarin_User20Xamarin_User20 Member ✭✭
    edited April 15

    I load the url of my website in xamarin iOS.

    And my webform project I have an video attribrute wich I define the src like this:

    video id="testVideo" runat="server" autoplay controls /video

    "testVideo.Attributes["src"] = "Showmedia.ashx?media=" + "small.mp4";"

    I hope this can help.

  • LandLuLandLu Member, Xamarin Team Xamurai

    Try to create a sample with it to help me reproduce this issue.

  • Xamarin_User20Xamarin_User20 Member ✭✭

    Hi LandLu,

    I have changed my code to using an local webview (because I think that is the reason my video will not load ) and I have found problem.

    When i try to load the iOS project now i get can TypeInitializationexception.

    this is the error:

    {System.ArgumentException: Default value did not match return type. Property: System.String MyHybridWebView.Url Default value type: RuntimeType, Parameter name: defaultValue at Xamarin.Forms.BindableProperty..ctor (System.String propertyName, System.Type ret…}

    This is my code :smile:

    mainpage.xaml:


    my webview class:

    public static readonly BindableProperty UrlProperty = BindableProperty.Create(
    propertyName: "Url",
    returnType: typeof(String),
    declaringType: typeof(MyHybridWebView),
    defaultValue: typeof(String));

    public string Url
    {
    get { return (String)GetValue(UrlProperty); }
    set { SetValue(UrlProperty, value); }
    }

    my webview renderer class:

    [assembly: ExportRenderer(typeof(MyHybridWebView), typeof(WkWebViewRenderer))]
    namespace iOSSkills.iOS
    {
    public class WkWebViewController : ViewRenderer<MyHybridWebView, WKWebView>
    {

    WKUserContentController userController;
    protected override void OnElementChanged(ElementChangedEventArgs e)
    {

    base.OnElementChanged(e);

    this.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;

    if (Control == null)
    {
    userController = new WKUserContentController();
    var config = new WKWebViewConfiguration();
    config.MediaPlaybackRequiresUserAction = false;
    config.AllowsInlineMediaPlayback = true;
    var webView = new WKWebView(Frame, config);
    webView.ScrollView.ScrollEnabled = false;
    SetNativeControl(webView);
    }
    else
    {
    Control.LoadRequest(new NSUrlRequest(new NSUrl(Element.Url)));
    }

    }

    Do you know how this problem can be solved?

    If so think my video problem will also be solved.

  • LandLuLandLu Member, Xamarin Team Xamurai

    Try to adjust the creation of bindable property like:

    public static readonly BindableProperty UrlProperty = BindableProperty.Create(
                                                            propertyName: "Url",
                                                            returnType: typeof(string),
                                                            declaringType: typeof(MyHybridWebView),
                                                            defaultValue: string.Empty);
    

    defaultValue should be a value instead of a type.

Sign In or Register to comment.