WebView with local file: how pass parameters?

Hi guys,
I have a problem with WebView. I want to pass some parameters to page that I load from local file such as

I have a solution for iOS:

string fileName = Path.Combine(NSBundle.MainBundle.BundlePath, string.Format("Content/{0}", "player.html"));

 var uri = new Uri(fileName);
 var safeHostUri = new Uri($"{uri.Scheme}://{uri.Authority}", UriKind.Absolute);
 var safeRelativeUri = new Uri($"{uri.PathAndQuery}{uri.Fragment}?{Element.Uri.Replace("player.html?", "")}", UriKind.Relative);

 // here I can pass the file and my parameters
 Control.LoadRequest(new NSUrlRequest(new Uri(safeHostUri, safeRelativeUri)));

JavaScript in the page

    function getParameterByName(name) {
        url = window.location.href;
        name = name.replace(/[\[\]]/g, '\\$&');
        var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
            results = regex.exec(url);

        if (!results) return null;
        if (!results[2]) return '';

        return decodeURIComponent(results[2].replace(/\+/g, ' '));
    }

Android implementation

public class HybridWebViewRenderer : ViewRenderer<HybridWebView, Android.Webkit.WebView>
{
    const string JavascriptFunction = "function invokeCSharpAction(data){jsBridge.invokeAction(data);}";
    Context _context;
    private static Android.Webkit.WebView webView;

    public HybridWebViewRenderer(Context context) : base(context)
    {
        _context = context;
    }

    protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView> e)
    {
        base.OnElementChanged(e);

        try
        {
            if (webView == null)
            {
                var webViewer = new Android.Webkit.WebView(_context);
                webViewer.Settings.JavaScriptEnabled = true;
                webViewer.SetWebViewClient(new JavascriptWebViewClient($"javascript: {JavascriptFunction}"));
                SetNativeControl(webViewer);
                webView = Control;
            }

            if (e.NewElement != null)
            {
                // here I want to pass some parameters
                webView.LoadUrl($"file:///android_asset/Content/player.html");
            }
            else
            {
                webView.LoadUrl($"file:///android_asset/Content/empty.html");
                webView = null;
            }
        }
        catch (Exception ex)
        {

        }
    }
}

UWP

public class HybridWebViewRenderer : ViewRenderer<HybridWebView, Windows.UI.Xaml.Controls.WebView>
{
    const string JavaScriptFunction = "function invokeCSharpAction(data){window.external.notify(data);}";
    private static WebView webView;

    protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView> e)
    {
        base.OnElementChanged(e);

        try
        {
            if (webView == null)
            {
                SetNativeControl(new WebView());

                webView = Control;
            }

            if (e.NewElement != null)
            {
                // here I want to pass some parameters
                webView.Source = new Uri(string.Format("ms-appx-web:///Content//{0}", Element.Uri));
            }
            else
            {
                webView.Source = new Uri(string.Format("ms-appx-web:///Content//{0}", "empty.html"));
                webView = null;
            }
        }
        catch (Exception ex)
        {

        }
    }

    async void OnWebViewNavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
    {
        if (args.IsSuccess)
        {
            await Control.InvokeScriptAsync("eval", new[] { JavaScriptFunction });
        }
    }

    void OnWebViewScriptNotify(object sender, NotifyEventArgs e)
    {
        Element.InvokeAction(e.Value);
    }
}

In the player.html I read the param from JavaScript. How can I do the same in Android and UWP?

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    I'm afraid you can't do this on android and uwp ,and LoadRequest is deprecated on ios, it is not recommended to use .

  • EnricoRossiniEnricoRossini USMember ✭✭✭✭
    edited April 4

    Hi @ColeX
    thank you! What is the best way to pass parameters then?

    I tried to use webView.EvaluateJavascript but it can't find the function in the script. Also, in Android there are some references to css and javascript on web such as

    If I look on the output window in Visual Studio, I can't see errors because the app is trying to find those files locally.

  • ColeXColeX Member, Xamarin Team Xamurai

    EvaluateJavascript method is the proper way i can think , but i know little about it ,sorry..

Sign In or Register to comment.