Delete Web Cache / Javascript Storage

bswanbswan ✭✭USMember ✭✭

I have an app that is using an http server to serve files to a Web View. The web viewers are caching image links which is causing broken images when their paths changes.

I can delete the web store on Android and UWP but I cannot figure out how to properly with iOS.

Android:

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

UWP:

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

I have tried the following with no luck:

        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;

Best Answer

  • bswanbswan ✭✭ US ✭✭
    Accepted Answer

    Found the answer at: https://gochannel.org/links/link/snapshot/640

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

Answers

  • bswanbswan ✭✭ USMember ✭✭
    Accepted Answer

    Found the answer at: https://gochannel.org/links/link/snapshot/640

    Rewrote to 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}");
                    }
                }
            }
    
  • NMackayNMackay mod GBInsider, University mod

    @bswan

    Thanks for sharing.

Sign In or Register to comment.