Xamarin Forms删除Web缓存/ Javascript存储

时间:2019-01-31 16:02:31

标签: xamarin.forms webview xamarin.ios wkwebview

我有一个使用HTTP服务器的文件用于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;

1 个答案:

答案 0 :(得分: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}");
            }
        }
    }