Xamarin Forms 删除 Web 缓存/Javascript 存储

Xamarin Forms Delete Web Cache / Javascript Storage

我有一个应用程序使用 http 服务器将文件提供给 Web 视图。 Web 查看器正在缓存图像链接,这会在路径更改时导致图像损坏。

我可以删除 Android 和 UWP 上的网上商店,但我不知道如何正确使用 iOS。

Android:

Android.Webkit.WebStorage.Instance.DeleteAllData();

UWP:

Windows.UI.Xaml.Controls.WebView.ClearTemporaryWebDataAsync();

我尝试了以下方法但没有成功:

 NSHttpCookieStorage.SharedStorage.RemoveCookiesSinceDate(NSDate.DistantPast);
            WKWebsiteDataStore.DefaultDataStore.FetchDataRecordsOfTypes(WKWebsiteDataStore.AllWebsiteDataTypes, (NSArray records) =>
            {
                for (nuint i = 0; i < records.Count; i++)
                {
                    var record = records.GetItem<WKWebsiteDataRecord>(i);

                    WKWebsiteDataStore.DefaultDataStore.RemoveDataOfTypes(
                    websiteDataTypes: record.DataTypes,
                    date: new[] { record },
                    completionHandler: ()=> { });
                }

                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}"); });
                }
            });

            NSUrlCache.SharedCache.RemoveAllCachedResponses();   
            NSUrlCache.SharedCache.DiskCapacity = 0;
            NSUrlCache.SharedCache.MemoryCapacity = 0;

在以下位置找到答案:https://gochannel.org/links/link/snapshot/640

重写为 Xamarin IOS

    private void DeleteCachedFiles()
    {

        if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
        {
            NSHttpCookieStorage.SharedStorage.RemoveCookiesSinceDate(NSDate.DistantPast);
            WKWebsiteDataStore.DefaultDataStore.FetchDataRecordsOfTypes(WKWebsiteDataStore.AllWebsiteDataTypes, (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}"); });
                }
            });

            NSUrlCache.SharedCache.RemoveAllCachedResponses();
        }
        else
        {

            // Remove the basic cache.
            NSUrlCache.SharedCache.RemoveAllCachedResponses();
            var cookies = NSHttpCookieStorage.SharedStorage.Cookies;

            foreach (var c in cookies)
            {
                NSHttpCookieStorage.SharedStorage.DeleteCookie(c);
            }
        }


        try
        {
            // Clear web cache
            DeleteLibraryFolderContents("Caches");

            // Remove all cookies stored by the site. This includes localStorage, sessionStorage, and WebSQL/IndexedDB.
            DeleteLibraryFolderContents("Cookies");

            // Removes all app cache storage.
            DeleteLibraryFolder("WebKit");

        }
        catch (Exception ex)
        {
            App.UnhandledException(ex, $"Error deleting cache {ex.Message}");
        }

    }

    private void DeleteLibraryFolder(string folderName)
    {
        var manager = NSFileManager.DefaultManager;
        var library = manager.GetUrls(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomain.User).First();
        var dir = Path.Combine(library.Path, folderName);

        manager.Remove(dir, out NSError error);
        if (error != null)
        {
            App.UnhandledException(new Exception(error.Description), error.Description);
        }
    }

    private void DeleteLibraryFolderContents(string folderName)
    {
        var manager = NSFileManager.DefaultManager;
        var library = manager.GetUrls(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomain.User).First();
        var dir = Path.Combine(library.Path, folderName);
        var contents = manager.GetDirectoryContent(dir, out NSError error);
        if (error != null)
        {
            App.UnhandledException(new Exception(error.Description), error.Description);
        }

        foreach (var c in contents)
        {
            try
            {
                manager.Remove(Path.Combine(dir, c), out NSError errorRemove);
                if (errorRemove != null)
                {
                    App.UnhandledException(new Exception(error.Description), error.Description);
                }
            }
            catch (Exception ex)
            {
                App.UnhandledException(ex, $"Error deleting folder contents: {folderName}{Environment.NewLine}{ex.Message}");
            }
        }
    }