Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Failing to load web page

Can anyone please advise reason why Google.com and Yahoo.com are loading fine on webview but failing to load other websites.

What could be possible reasons?

These websites are loading on chrome but not loading on webview.

Thanks in advance.

Tagged:

Best Answer

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited December 2020 Accepted Answer

    @chathar , your attribute in custom renderer is wrong, becuase you use webview in the xaml , Not HybridWebView, please change the [assembly: ExportRenderer(typeof(HybridWebView), typeof(HybridWebViewRenderer))] to [assembly: ExportRenderer(typeof(WebView), typeof(HybridWebViewRenderer))] like following screenshot


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @chathar Can you share your test url? Just load other websites, scope is too widely.

    I test with https://stackoverflow.com/ and https://www.youtube.com/, Both of them are running normally in the webview.

    If you want to access the Http url, In the android, please add android:usesCleartextTraffic="true" in the <application> tag of AndroidManifest.xml like following code.

     <application android:label="HorizontalCollectionView.Android" android:theme="@style/MainTheme" android:usesCleartextTraffic="true"></application>
    

    In the iOS, you can make your application to Opting-Out of ATS

    https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/ats#opting-out-of-ats


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • chatharchathar Member ✭✭

    Hi @LeonLu,

    i am able to load your mentioned pages but when i call https:// 66.85.30.135 (please remove spaces as i is not allowing me to post url)

    similarly some other pages simply show me blank white page.

    i have android:usesCleartextTraffic="true" in place.

    Regards,

    Athar

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Do you want to load following page?

    If so, please add an custom-renderer webview to fix this issue.

    [assembly: ExportRenderer(typeof(WebView), typeof(HybridWebViewRenderer))]
    namespace HorizontalCollectionView.Droid
    {
        class HybridWebViewRenderer : WebViewRenderer
        {
            public HybridWebViewRenderer(Context context) : base(context)
            {
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
            {
                base.OnElementChanged(e);
                Control.Settings.JavaScriptEnabled = true;
    
                Control.SetWebViewClient(new MyWebviewClient());
            }
        }
    
        internal class MyWebviewClient : Android.Webkit.WebViewClient
        {
            public override void OnReceivedClientCertRequest(Android.Webkit.WebView view, Android.Webkit.ClientCertRequest request)
            {
                request.Ignore();
                base.OnReceivedClientCertRequest(view, request);
            }
            public override void OnReceivedSslError(Android.Webkit.WebView view, Android.Webkit.SslErrorHandler handler, SslError error)
            {
                handler.Proceed();
                base.OnReceivedSslError(view, handler, error);
    
            }
        }
    }
    

    Here is my xaml layout.

      <WebView Source="https://66.85.30.135/" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" />
    

    And Add following permission:

    <uses-permission android:name="android.permission.INTERNET" />
    
  • chatharchathar Member ✭✭

    Thanks, due to some odd reason it is not working with me.

    i am attaching herewith my solution file. can you please review and advise?

    https:// 1drv.ms/u/s!AoOYe58xRMNSktwNZ-odGBM0EJRYXg?e=9e5faK

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited December 2020 Accepted Answer

    @chathar , your attribute in custom renderer is wrong, becuase you use webview in the xaml , Not HybridWebView, please change the [assembly: ExportRenderer(typeof(HybridWebView), typeof(HybridWebViewRenderer))] to [assembly: ExportRenderer(typeof(WebView), typeof(HybridWebViewRenderer))] like following screenshot


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    If reply is helpful, please click the Yes tab under the helpful answer.It will help others who have similar issue.


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • chatharchathar Member ✭✭

    wow, you are the best!! thanks mate!!

Sign In or Register to comment.