1

I need to pass a query string into a WebView Xamarin Source on Navigating (www.websiteexmple.com?mobileapp=true).

In Android works perfect, but in IOS does not seems to work correctly sometimes . I am missing someting ?

I have notice it does not work in IOS when Net Core website needs to do some redirects : Ex: User Enter Email and click Next Step.

 public void WebView_NavigatingAsync(object sender, WebNavigatingEventArgs args)
    {
        if (!args.Url.Contains(Source) && !args.Url.Contains("facebook") && !args.Url.Contains("google"))
        {
            var uri = new Uri(args.Url);
            // Device.OpenUri(uri);
            Launcher.OpenAsync(uri);
            args.Cancel = true;
        }
        else
        {
            var src = args.Url;
            string prefix = "?";
            if (src.Contains("?"))
            {
                prefix = "&";
            }
            if (!args.Url.Contains("mobileapp=true"))
            {
                args.Cancel = true;
                var webview = (WebView)sender;
                webview.Source = src + prefix + "mobileapp=true";                   
            }
        }
    }
2
  • Hi , you mean the args.Url.Contains does not work ? Commented Jun 22, 2020 at 5:48
  • Hi, This Code add a query string to the end of the URL . Example: www.netcorewebsite.com?mobileapp=true and it works perfect in Android . But doesn;t work in in IOS when the .Net Core Website needs to do a redirect. Ex User enter Email and click Next Step . Commented Jun 22, 2020 at 6:05

1 Answer 1

1

I think there is a bug in WebView.Source for iOS, the question mark is coming encoded and the system didn't recognize the URL. You can create a custom WebViewRender and replace %3F to ?

On iOS project create a CustomWebViewRender

[assembly: ExportRenderer(typeof(CustomWebView), typeof(CustomWebViewRenderer))]

namespace Test.iOS.Renders
{
    public class CustomWebViewRenderer : WkWebViewRenderer
    {

        public override WKNavigation LoadRequest (NSUrlRequest request)
        {
            var url = request.Url.ToString().Replace("html%3F", "html?");
            var dotnetURI = new System.Uri(url);

            var idn = new System.Globalization.IdnMapping();
            NSUrl nsURL = new NSUrl(dotnetURI.Scheme, idn.GetAscii(dotnetURI.DnsSafeHost), dotnetURI.PathAndQuery);
               
            return base.LoadRequest(new NSUrlRequest(nsURL));
        }
    }
}

On Xamarin project use your custom WebView

cs file:

namespace Test.Renders
{
    public class CustomWebView : WebView
    {
    }
}

Xaml file:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:test="clr-namespace:Test.Renders"
         ...>
<ContentPage.Content>
    <StackLayout>
        <test:CustomWebView x:Name="map"
                           HorizontalOptions="FillAndExpand"
                           VerticalOptions="FillAndExpand"/>
  ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.