locked
No action on URL Redirect RRS feed

  • Question

  • User390394 posted

    If I load an URL (https://www.hurriyet.com/) _on the webview, there is no result but in my pc's web browser redirects it to _https://www.hurriyet.com.tr.

    Webview doesn't show any generic error or some other thing.

    Is it a webview bug?

    Monday, January 6, 2020 1:48 PM

Answers

  • User369979 posted

    The certificate of this website is not trusted so it can't be displayed in the webview on Android. If you do want to use this https://www.hurriyet.com/, firstly, try to create a custom web view:

    public class MyWebView : WebView
    {
        public static readonly BindableProperty UrlProperty = BindableProperty.Create(nameof(Url), typeof(string), typeof(MyWebView));
        public string Url
        {
            set => SetValue(UrlProperty, value);
            get => (string)GetValue(UrlProperty);
        }
    }
    

    Then define a custom renderer for this control in Android:

    [assembly: ExportRenderer(typeof(MyWebView), typeof(CustomWebViewRenderer))]
    namespace App.Droid
    {
        public class CustomWebViewRenderer : WebViewRenderer
        {
            Context _context;
            public CustomWebViewRenderer(Context context) : base(context)
            {
                _context = context;
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
            {
                base.OnElementChanged(e);
    
                if (e.NewElement != null)
                {
                    Android.Webkit.WebView web_view = new Android.Webkit.WebView(_context);
                    web_view.LoadUrl(((MyWebView)Element).Url);
                    web_view.SetWebViewClient(new MyWebViewClient());
                    SetNativeControl(web_view);
                }
            }
        }
    
        public class MyWebViewClient : WebViewClient
        {
            public override void OnReceivedSslError(Android.Webkit.WebView view, SslErrorHandler handler, SslError error)
            {
                handler.Proceed();
            }
        }
    }
    

    At last, consume it on content page like:

    <StackLayout>
        <local:MyWebView VerticalOptions="FillAndExpand" Url="https://www.hurriyet.com/"/>
    </StackLayout>
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, January 7, 2020 5:49 AM

All replies

  • User369979 posted

    The certificate of this website is not trusted so it can't be displayed in the webview on Android. If you do want to use this https://www.hurriyet.com/, firstly, try to create a custom web view:

    public class MyWebView : WebView
    {
        public static readonly BindableProperty UrlProperty = BindableProperty.Create(nameof(Url), typeof(string), typeof(MyWebView));
        public string Url
        {
            set => SetValue(UrlProperty, value);
            get => (string)GetValue(UrlProperty);
        }
    }
    

    Then define a custom renderer for this control in Android:

    [assembly: ExportRenderer(typeof(MyWebView), typeof(CustomWebViewRenderer))]
    namespace App.Droid
    {
        public class CustomWebViewRenderer : WebViewRenderer
        {
            Context _context;
            public CustomWebViewRenderer(Context context) : base(context)
            {
                _context = context;
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
            {
                base.OnElementChanged(e);
    
                if (e.NewElement != null)
                {
                    Android.Webkit.WebView web_view = new Android.Webkit.WebView(_context);
                    web_view.LoadUrl(((MyWebView)Element).Url);
                    web_view.SetWebViewClient(new MyWebViewClient());
                    SetNativeControl(web_view);
                }
            }
        }
    
        public class MyWebViewClient : WebViewClient
        {
            public override void OnReceivedSslError(Android.Webkit.WebView view, SslErrorHandler handler, SslError error)
            {
                handler.Proceed();
            }
        }
    }
    

    At last, consume it on content page like:

    <StackLayout>
        <local:MyWebView VerticalOptions="FillAndExpand" Url="https://www.hurriyet.com/"/>
    </StackLayout>
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, January 7, 2020 5:49 AM
  • User390394 posted

    @LandLu, you are a lifesaver :)

    I've already had a custom webview, when I add the below method, it worked.

           public override void OnReceivedSslError(WebView view, SslErrorHandler handler, SslError error)
            {
                //base.OnReceivedSslError(view, handler, error);
                handler.Proceed();
            }
    

    Thanks

    Tuesday, January 7, 2020 8:01 AM