Setting WebView AllowFileAccess property in Xamarin Forms Shared Project

Raaz Paul 1 Reputation point
2021-06-12T14:46:07.967+00:00

Hi,

How can I want to set below webview settings in shared project

webView.Settings.AllowContentAccess = true;
webView.Settings.AllowFileAccess = true;

Below is the XAML part

<WebView x:Name="webView" Navigating="webView_Navigating" Navigated="webView_Navigated" ></WebView>

Tried custom renderer, but renderer OnElementChanged calls after webview source has been set

Thanks.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,352 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 75,581 Reputation points Microsoft Vendor
    2021-06-14T07:38:17.027+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You can create a custom webview add two BindableProperty in the background code.

       public class MyWebview:WebView  
           {  
               public static BindableProperty AllowContentAccessProperty =  
                            BindableProperty.Create(nameof(AllowContentAccess),  
                            typeof(bool),  
                            typeof(MyWebview),  
                            null);  
         
              
               public bool AllowContentAccess  
               {  
                   get => (bool)GetValue(AllowContentAccessProperty);  
                   set => SetValue(AllowContentAccessProperty, value);  
               }  
         
         
         
               public static BindableProperty AllowFileAccessProperty =  
                           BindableProperty.Create(nameof(AllowFileAccess),  
                           typeof(bool),  
                           typeof(MyWebview),  
                           null);  
         
         
               public bool AllowFileAccess  
               {  
                   get => (bool)GetValue(AllowFileAccessProperty);  
                   set => SetValue(AllowFileAccessProperty, value);  
               }  
           }  
    

    Then use it in the xaml.

       <customwebviewdemo:MyWebview AllowContentAccess="True" AllowFileAccess="True" Source="https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/entry" WidthRequest="100" HeightRequest="100" ></customwebviewdemo:MyWebview>  
    

    In the android customrenderer, you can set the Bindable value.

       [assembly: ExportRenderer(typeof(MyWebview), typeof(MyWebviewRenderer))]  
       namespace CustomWebviewDemo.Droid  
       {  
           class MyWebviewRenderer : WebViewRenderer  
           {  
               Context context;  
               public MyWebviewRenderer(Context context) : base(context)  
               {  
                   this.context = context;  
               }  
         
               protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)  
               {  
                   base.OnElementChanged(e);  
         
                   if (e.OldElement != null || Element == null)  
                   {  
                       return;  
                   }  
         
                   var webView = (global::Android.Webkit.WebView)Control;  
         
                   MyWebview myWebview= e.NewElement as MyWebview;  
                    
         
                    
         
                   webView.Settings.AllowContentAccess = myWebview.AllowContentAccess;  
                   webView.Settings.AllowFileAccess = myWebview.AllowFileAccess;  
         
                    
               }  
           }  
       }  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Raaz Paul 1 Reputation point
    2021-06-18T15:02:09.483+00:00

    Hi Leon Lu,

    Thank you for detailed code. I tried implement it same way, but it still gives same error.

    Apparently, webview goes to renderer after webview has been initialized, i.e. first it has called protected override void OnAppearing() {}, then protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e) {}

    This happening at random - for. e.g. If I open browser (navigation.PushModalAsync) 10 time (same url), it will give error randomly 2 to 3 times, other it will load page perfectly.

    Error :

    Webpage not available

    The webpage at
    file:///data/user/0/com.test.xamarin/files/webpage.html
    could not be loaded because:

    net:ERR_ACCESS_DENIED

    Below is my implementation :

    Custom Control (WebViewCustom) = same as yours.
    Rendererer = same as yours

    Below is WebBrowser Page XML that uses WebViewCustom

    <ContentPage.Content>
        <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <controls:WebViewCustom x:Name="wvBrowser" AllowContentAccess="True" AllowFileAccess="True" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"  ></controls:WebViewCustom>
        </StackLayout>
    </ContentPage.Content>
    

    WebBrowser CS

    public partial class WebViewBrowser : ContentPage
    {
        string url;
        public WebViewBrowser(string url)
        {
            InitializeComponent();
            this.url = url;
        }
    
        protected override void OnAppearing()
        {
            base.OnAppearing();
            wvBrowser.Source = new UrlWebViewSource { Url = this.url };
        }
    }
    

    }


  3. Raaz Paul 1 Reputation point
    2021-06-18T15:04:00.183+00:00

    Hi Leon Lu,

    Thank you for detailed code. I tried implement it same way, but it still gives same error.

    Apparently, webview goes to renderer after webview has been initialized, i.e. first it has called protected override void OnAppearing() {}, then protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e) {}

    Below is my implementation

    Custom Control (WebViewCustom) = same as yours.
    Rendererer = same as yours

    Below is WebBrowser Page XML that uses WebViewCustom

    <ContentPage.Content>
        <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <controls:WebViewCustom x:Name="wvBrowser" AllowContentAccess="True" AllowFileAccess="True" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"  ></controls:WebViewCustom>
        </StackLayout>
    </ContentPage.Content>
    

    WebBrowser CS

    public partial class WebViewBrowser : ContentPage
    {
        string url;
        public WebViewBrowser(string url)
        {
            InitializeComponent();
            this.url = url;
        }
    
        protected override void OnAppearing()
        {
            base.OnAppearing();
            wvBrowser.Source = new UrlWebViewSource { Url = this.url };
        }
    }
    

    }


  4. Derlidio Siqueira 1 Reputation point
    2022-03-11T18:16:01.043+00:00

    After doing all the custom renderer and configuration stuff, what solved the problem for me was adding android:usesCleartextTraffic="true" at <application> tag in AndroidManifest File.

    permissions: Read External Storage, Write External Storage, Query All Packages, Internet

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.