清除WebView缓存xamarin IOS

时间:2017-08-03 13:07:38

标签: c# ios xamarin wkwebview

我需要在关闭WebView后清除缓存 我使用Xamarin iOS clear cache from WKWebView中的代码 但它没有效果。已经了解records.Count equals 0

如何正确使用?

var websiteDataTypes = new NSSet<NSString>(new []
{
    //Choose which ones you want to remove
    WKWebsiteDataType.Cookies,
    WKWebsiteDataType.DiskCache,
    WKWebsiteDataType.IndexedDBDatabases,
    WKWebsiteDataType.LocalStorage,
    WKWebsiteDataType.MemoryCache,
    WKWebsiteDataType.OfflineWebApplicationCache,
    WKWebsiteDataType.SessionStorage,
    WKWebsiteDataType.WebSQLDatabases
});

WKWebsiteDataStore.DefaultDataStore.FetchDataRecordsOfTypes (websiteDataTypes, (NSArray records) =>
{
    for (nuint i = 0; i < records.Count; i++) {
        var record = records.GetItem<WKWebsiteDataRecord> (i);

        WKWebsiteDataStore.DefaultDataStore.RemoveDataOfTypes (record.DataTypes, 
            new[] {record}, () => {Console.Write($"deleted: {record.DisplayName}");});
    }
});

1 个答案:

答案 0 :(得分:1)

通过以下代码解决问题:

NSHttpCookieStorage CookieStorage = NSHttpCookieStorage.SharedStorage;

        foreach (var cookie in CookieStorage.Cookies)
            CookieStorage.DeleteCookie(cookie);