Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear UWP WebView cache?

I am using WebView in my UWP application and I want to clear the cache while closing the app, is there a way? I know I can disable cache by adding headers into my HttpRequestMessage as mentioned in this link. However, I want to be able to clear the cache upon app exit.

I did try WebView.ClearTemporaryWebDataAsync() without any success. Once something is cached it normally remains throughout the app. Any help is appreciated, thanks.

Edit : Adding code snippet

var webView = new WebView();  
webView.Navigate(new Uri("http://refreshyourcache.com/en/cache-test/"));  
await WebView.ClearTemporaryWebDataAsync(); //static method  
webView.Navigate(new Uri("http://refreshyourcache.com/en/cache-test/"));

I expect the static method to clear cache and when I navigate to same page again its cache should be cleared. Am I doing something wrong here?

like image 893
manjunath shetkar Avatar asked Jun 09 '16 15:06

manjunath shetkar


1 Answers

In UWP (XAML) there is the ClearTemporaryWebDataAsync method, that allows to webview's cache and IndexedDB data. And there is similar method for JavaScript in MSApp - clearTemporaryWebDataAsync.

Here is code sample (based on your one) that works for me:

XAML:

 <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
     <StackPanel>
         <WebView x:Name="webView" Width="800" Height="600"></WebView>
         <Button x:Name="refreshBtn" Content="Refresh" ></Button>
     </StackPanel>
 </Grid>

C#:

    public MainPage()
    {
        this.InitializeComponent();
        refreshBtn.Tapped += RefreshBtn_Tapped;

        webView.Navigate(new Uri("http://refreshyourcache.com/en/cache-test/"));


    }

    private async void RefreshBtn_Tapped(object sender, TappedRoutedEventArgs e)
    {
        await Windows.UI.Xaml.Controls.WebView.ClearTemporaryWebDataAsync();
        webView.Navigate(new Uri("http://refreshyourcache.com/en/cache-test/"));
    }

When I click refresh button, cache is cleared -- I see green image.

like image 134
Konstantin Avatar answered Sep 20 '22 19:09

Konstantin