Xamarin Android webview无法清除缓存

时间:2017-10-02 13:03:46

标签: xamarin xamarin.android android-webview

您好我正在尝试清除当前的WebView会话。它似乎无法正常工作。我在OnElementCacheClear中调用CustomWebviewRenderer方法。我可以看到WebView不是null。此方法被调用...当我打开WebView时,它仍然具有上一个登录会话。

public class WebViewCustomRenderer : WebViewRenderer
    {
        CustomXamrinWebView formsWebView;
        WebAndroid webview = new WebAndroid(Forms.Context);
        protected override void OnElementChanged (ElementChangedEventArgs<Xamarin.Forms.WebView> e)
        {
            base.OnElementChanged (e);
            if (Control == null)
            {
                webview.Settings.JavaScriptEnabled = true;
                webview.Settings.JavaScriptCanOpenWindowsAutomatically = false;
                webview.Settings.DomStorageEnabled = true;
                webview.Settings.MixedContentMode = MixedContentHandling.AlwaysAllow;
                webview.Settings.AllowFileAccessFromFileURLs = true;
                webview.Settings.AllowUniversalAccessFromFileURLs = true;
                webview.Settings.AllowContentAccess = true;
                webview.Settings.AllowFileAccess = true;
                webview.Settings.DatabaseEnabled = true;
                webview.Settings.UserAgentString = "Android WebView";
                webview.Settings.UseWideViewPort = true;
                webview.Settings.LoadsImagesAutomatically = true;
                webview.Settings.SetEnableSmoothTransition(true);
                webview.Settings.SetAppCacheEnabled(true);

                if (Build.VERSION.SdkInt < BuildVersionCodes.Kitkat)
                {
                                        string dbPath = this.Context.GetDir("database", FileCreationMode.Private).Path;
                    webview.Settings.DatabasePath = dbPath;
                }

                SetNativeControl(webview);

            }

            if (Control != null)
            {
                Control.Settings.DomStorageEnabled = true;
            }

            if (e.NewElement != null) {
                            formsWebView = e.NewElement;
                formsWebView.CacheClearRequested += OnElementCacheClear;
                Control.SetWebViewClient(new CustomWebViewClient(formsWebView));
            }

            if (e.OldElement != null)
            {
                 CustomXamrinWebView oldWebview = e.NewElement;
                                 oldWebview.CacheClearRequested -= OnElementCacheClear;
            }
        }

        private void OnElementCacheClear(object sender, EventArgs e)
        {
            /*   if(webview != null) {
                   System.Diagnostics.Debug.WriteLine("Cache clear called ");
               }
               System.Diagnostics.Debug.WriteLine("Cache clear called ");

               webview.ClearCache(true);
               webview.ClearFormData();
               webview.ClearHistory();
               webview.ClearCache(true);

               CookieManager cookieManager = CookieManager.Instance;
               cookieManager.RemoveAllCookie();
               cookieManager.RemoveSessionCookie();
               this.Context.DeleteDatabase("webview.db");
               this.Context.DeleteDatabase("webviewCache.db");
               webview.LoadUrl(ServiceUrl.loginUrl);
               //webview.Destroy(); */
            CookieManager cookieManager = CookieManager.Instance;
            if (cookieManager.HasCookies)
            {
                if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
                {
                    cookieManager.RemoveAllCookies(null);
                }
                else
                {
                    cookieManager.RemoveAllCookie();
                }
            }
            Device.BeginInvokeOnMainThread(() => new WebAndroid(Forms.Context).ClearCache(true));
        }
}

3 个答案:

答案 0 :(得分:0)

试试这个(它不是WebView特定于实例的):

/// <summary>
/// See interface summary.
/// </summary>
public void DeleteAllCookies() {

    if(_cookieManager.HasCookies) {
        if(Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop) {
            _cookieManager.RemoveAllCookies(null);
        } else {
            _cookieManager.RemoveAllCookie();
        }
    }

    Device.BeginInvokeOnMainThread(() => new Android.Webkit.WebView(Forms.Context).ClearCache(true));
}

答案 1 :(得分:0)

它可能是一个较旧的线程,但可能对其他人还是有帮助的。

这是我解决的方法,避免始终关闭应用程序以结束WebView的会话。

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

/// <summary>
/// Delete Cookies and clear Cache
/// </summary>
public void DeleteCC()
{
    if (Android.Webkit.CookieManager.Instance.HasCookies)
    {
        if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop)
            Android.Webkit.CookieManager.Instance.RemoveAllCookies(null);
        else
            Android.Webkit.CookieManager.Instance.RemoveAllCookie();
    }

    Device.BeginInvokeOnMainThread(() => new Android.Webkit.WebView(Android.App.Application.Context).ClearCache(true));

    // Refresh the current page by replacing it with a new instance.
    Application.Current.MainPage = new NavigationPage(new MyPage());
}´´´

答案 2 :(得分:0)

xamarin表单实现可以这样工作:

  //Your view
  protected override void OnAppearing()
    {
        base.OnAppearing();

        //your named webview
        ReportWebView.Cookies = new CookieContainer(1);
    }