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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With