Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose platform webview for initialization to enable customization #45

Closed
Eilon opened this issue Feb 10, 2024 · 4 comments · Fixed by #53
Closed

Expose platform webview for initialization to enable customization #45

Eilon opened this issue Feb 10, 2024 · 4 comments · Fixed by #53

Comments

@Eilon
Copy link
Owner

Eilon commented Feb 10, 2024

For example, in #27 (comment) this was mentioned:

Can I disable Android WebView TextZoom on this? I can set it by use blazorWebView in the Init event:

                private void blazorWebView_BlazorWebViewInitialized(object sender, Microsoft.AspNetCore.Components.WebView.BlazorWebViewInitializedEventArgs e)
                {
        #if ANDROID
                    e.WebView.Settings.TextZoom = 100;
                   e.WebView.Settings.SetSupportZoom(false);
        #endif
                }

but It seems private in your library =>private Android.Webkit.WebView PlatformWebView

@pagirre
Copy link

pagirre commented Feb 14, 2024

That would be great as I have had to modify a local copy of your codebase to add this tweaks between MODIFICATION BEGIN END comments:

private partial Task InitializeHybridWebView()
{
    // Note that this is a per-app setting and not per-control, so if you enable
    // this, it is enabled for all Android WebViews in the app.
    AWebView.SetWebContentsDebuggingEnabled(enabled: EnableWebDevTools); 
    PlatformWebView.Settings.JavaScriptEnabled = true;

    //MODIFICATION BEGIN
    PlatformWebView.Settings.AllowUniversalAccessFromFileURLs = true;
    PlatformWebView.Settings.AllowFileAccessFromFileURLs = true;
    PlatformWebView.Settings.AllowFileAccess = true;
    PlatformWebView.Settings.AllowContentAccess = true;
    //MODIFICATION END

    _javaScriptInterface = new HybridWebViewJavaScriptInterface(this);
    PlatformWebView.AddJavascriptInterface(_javaScriptInterface, "hybridWebViewHost");

    return Task.CompletedTask;
}

Thank you for your effort, Eilon

@pagirre
Copy link

pagirre commented Feb 15, 2024

Hi again.

I was convinced that doing these little tweaks(exposing those 5 fields just to check their value when navigating) in your code,

image

I would be able to avoid CORS (in the next screenshot you can check that all the abovementioned fields are set to true while Navigating event)

image

But I am afraid I have not succeeded:

image

Any help appreciated. Cheers!

@pagirre
Copy link

pagirre commented Feb 15, 2024

Forget about my last post. It is working now.

The problem was that a colleague did not provide me the full information: the origin URI should have been localhost:3000 instead of localhost. That is what, I guess, is allowed to avoid CORS error.

But as you can see, AppHostAddress could be another member that I would like to set from outside at the beginning.

@Eilon
Copy link
Owner Author

Eilon commented Mar 12, 2024

This feature is merged for 1.0.0-preview6.

Customizing web view initialization

To customize the platform web view you can add an event handler for the new HybridWebViewInitialized event:

    myHybridWebView.HybridWebViewInitialized += MyHybridWebView_WebViewInitialized;

    ...

    private void MyHybridWebView_WebViewInitialized(object sender, HybridWebViewInitializedEventArgs e)
    {
        // Use e.WebView to customize things
    }

And in that event handler you can access the platform-specific properties and methods of the web view control. This code would typically be in an #if SOME_PLATFORM ... #endif block because the APIs are different for each platform, and this enables platform-specific code to be compiled for only that platform.

For example, this event handler customizes the zoom settings on Windows and Android, and does nothing for iOS and macOS:

    private void MyHybridWebView_WebViewInitialized(object sender, HybridWebViewInitializedEventArgs e)
    {
#if WINDOWS
        // Disable the user manually zooming
        e.WebView.CoreWebView2.Settings.IsZoomControlEnabled = false;
#elif ANDROID
        // Customize zoom options
        e.WebView.Settings.TextZoom = 200;
        e.WebView.Settings.SetSupportZoom(false);
#endif
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants