locked
WebView Link Blocking if Contains RRS feed

  • Question

  • User382371 posted

    I have to do a function in WebView, first my app on start loaded Google, then when I declared a value to block facebook, when i search it on google it must not do something and just stay on google or even if I search 'fb', google can show results with facebook but when it is clicked it must cancel navigating. My code are below which doesn't work, its for xamarin android onlu. Please help me. Thank you

    Monday, February 18, 2019 11:07 PM

All replies

  • User380718 posted

    in ShouldOverrideUrlLoading method ,you should write like this to Intercept navigation :

     public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
            {
                if (request.Url.ToString().Contains("facebook"))
                {
                    return true;
                }
                else
                {
                    view.LoadUrl(request.Url.ToString());
                    return true;
                }
            }
    
    Tuesday, February 19, 2019 7:17 AM
  • User382371 posted

    Why do you use 'return true' in else statement?

    Tuesday, February 19, 2019 7:32 AM
  • User380718 posted

    because before return ,you have use view.LoadUrl(request.Url.ToString()); if you return false ,it will loadUrl, then you will see that the page refreshes twice . return false is equal to view.LoadUrl(request.Url.ToString());return true; return true indicates that the current url will not execute even if it is a redirect url

    Tuesday, February 19, 2019 9:24 AM
  • User382371 posted

    But Still it isn't working

    Thursday, February 21, 2019 6:46 AM
  • User380718 posted

    What's the problem now, or does typing facebook navigate to a new page

    Thursday, February 21, 2019 8:11 AM
  • User382371 posted

    Yes, it still navigating

    Thursday, February 21, 2019 8:13 AM
  • User380718 posted

    could you show me the codes of the whole webview activity

    Thursday, February 21, 2019 8:28 AM
  • User382371 posted

    using Android.App; using Android.OS; using Android.Support.V7.App; using Android.Runtime; using Android.Widget; using Android.Webkit; using static Android.Views.View; using System; using Android.Views; using Android.Support.Design.Widget;

    namespace WebBLockerr { [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)] public class MainActivity : Activity {

        WebView browser;
        Button goButton, backButton, previewButton;
        EditText txtUrl; 
    
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
    
            browser = FindViewById<WebView>(Resource.Id.webbyu);
            goButton = FindViewById<Button>(Resource.Id.search);
            backButton = FindViewById<Button>(Resource.Id.back);
            previewButton = FindViewById<Button>(Resource.Id.forward);
            txtUrl = FindViewById<EditText>(Resource.Id.textUrl);
            txtUrl.Text = "http://www.google.com";
    
            WebSettings webSettings = browser.Settings;
            webSettings.JavaScriptEnabled = true;
    
            browser.SetWebViewClient(new ExtendView());
            browser.LoadUrl("http://www.google.com");
    
            goButton.Click += goSearch;
            backButton.Click += back;
            previewButton.Click += preview;
        }
    
        private void preview(object sender, EventArgs e)
        {
            if (browser.CanGoForward())
                browser.GoForward();
        }
    
        private void back(object sender, EventArgs e)
        {
            if (browser.CanGoBack())
                browser.GoBack();
        }
    
        private void goSearch(object sender, EventArgs e)
        {
            if (!txtUrl.Text.Contains("http://"))
            {
                string address = txtUrl.Text;
                txtUrl.Text = String.Format("http://{0}", address);
            }
            browser.LoadUrl(txtUrl.Text);
        }
    
        public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent e)
        {
            if(keyCode == Keycode.Back && browser.CanGoBack())
            {
                browser.GoBack();
                return true;
            }
            return base.OnKeyDown(keyCode, e);
        }
    }
    
    public class ExtendView : WebViewClient
    {
        public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
        {
            if (request.Url.ToString().Contains("facebook"))
            {
                return true;
            }
            else
            {
                view.LoadUrl(request.Url.ToString());
                return true;
            }
        }
    }
    

    }

    Thursday, February 21, 2019 8:33 AM
  • User380718 posted

    i will test again

    Friday, February 22, 2019 7:40 AM
  • User382371 posted

    Thank you

    Friday, February 22, 2019 7:43 AM
  • User382371 posted

    @LeoZhu Have you found any solution about this?

    Monday, February 25, 2019 5:37 AM
  • User380718 posted

    @diockman i test that,if type in the Browser in webview(e.g. use webview open google,then search "facebook"),it will work,but if type in an editText,then click button to use webview.loadUrl(),it doesnt work,no good solutions have been found.Maybe I'm missing something. I'm still trying

    Monday, February 25, 2019 5:44 AM
  • User382371 posted

    Okay thank you!

    Monday, February 25, 2019 5:52 AM