nmoschkin / MAUIWebViewExample

MAUI Custom Renderer For Hybrid Web View
MIT License
56 stars 17 forks source link

WebView settings #6

Open vovanb opened 2 years ago

vovanb commented 2 years ago

I try to run inside of MAUI WebView existing portal based on AngularJS which is working fine in default browser. It is failing due to rendering issues. I found recommendation in https://stackoverflow.com/questions/35443149/issues-in-loading-angular-js-website-in-android-webview to enable HTML5. I tried to do it in your HybridWebViewHandler.cs as below:

 protected override Android.Webkit.WebView CreatePlatformView()

   {
            sync = sync ?? SynchronizationContext.Current;

            var webView = new Android.Webkit.WebView(Context);
            jsBridgeHandler = new JSBridge(this);

            //enable HTML5
            webView.Settings.JavaScriptEnabled = true;

            webView.Settings.AllowFileAccess = true;
            webView.Settings.SetSupportMultipleWindows(true);
            webView.Settings.DatabaseEnabled = true;
            webView.Settings.DatabasePath = "/data/data/" + "/databases/";

            webView.Settings.DomStorageEnabled = true;
            webView.Settings.SetAppCacheEnabled(true);
            webView.Settings.SetAppCacheMaxSize(1024 * 1024 * 8);
            webView.Settings.SetAppCachePath("/data/data/cache/");
            webView.ClearCache(true);

           //end of enable HTML5
            webView.SetWebViewClient(new JavascriptWebViewClient($"javascript: {JavascriptFunction}"));
            webView.AddJavascriptInterface(jsBridgeHandler, "jsBridge");

            return webView;
        }

But it did not help... Is it right place to add? What I am missing and what can be improved?

nmoschkin commented 2 years ago

I try to run inside of MAUI WebView existing portal based on AngularJS which is working fine in default browser. It is failing due to rendering issues. I found recommendation in https://stackoverflow.com/questions/35443149/issues-in-loading-angular-js-website-in-android-webview to enable HTML5. I tried to do it in your HybridWebViewHandler.cs as below:

 protected override Android.Webkit.WebView CreatePlatformView()

   {
            sync = sync ?? SynchronizationContext.Current;

            var webView = new Android.Webkit.WebView(Context);
            jsBridgeHandler = new JSBridge(this);

            //enable HTML5
            webView.Settings.JavaScriptEnabled = true;

            webView.Settings.AllowFileAccess = true;
            webView.Settings.SetSupportMultipleWindows(true);
            webView.Settings.DatabaseEnabled = true;
            webView.Settings.DatabasePath = "/data/data/" + "/databases/";

            webView.Settings.DomStorageEnabled = true;
            webView.Settings.SetAppCacheEnabled(true);
            webView.Settings.SetAppCacheMaxSize(1024 * 1024 * 8);
            webView.Settings.SetAppCachePath("/data/data/cache/");
            webView.ClearCache(true);

           //end of enable HTML5
            webView.SetWebViewClient(new JavascriptWebViewClient($"javascript: {JavascriptFunction}"));
            webView.AddJavascriptInterface(jsBridgeHandler, "jsBridge");

            return webView;
        }

But it did not help... Is it right place to add? What I am missing and what can be improved?

I'm not sure I follow. I can do some investigating if you want, but that isn't in the scope of the problem this sample is attempting to solve. The best thing to do would be to consult more generally how HTML5 gets enabled in Android WebView widgets, and then try to adapt the code samples to your usage. If there's a Java sample, there's most likely an equivalent way it can be written in C#. The abstraction is pretty complete.

nmoschkin commented 2 years ago

Have you tried invoking it on main thread?

sync.Send or sync.Post?