How to delete cache and cookies?

For DesktopHTMLViever, it seems that there is no way to delete cache and cookie before to load an url. Any declare working for all versions of macOS?

For clearing cache, AI give me that but cannot find a reliable code for cookie.

Declare Function NSClassFromString Lib "Foundation" (className As CFStringRef) As Ptr
Declare Function sharedURLCache Lib "Foundation" Selector "sharedURLCache" (cls As Ptr) As Ptr
Declare Sub removeAllCachedResponses Lib "Foundation" Selector "removeAllCachedResponses" (cache As Ptr)


Dim NSURLCacheClass As Ptr = NSClassFromString("NSURLCache")
If NSURLCacheClass <> Nil Then
  Dim sharedCache As Ptr = sharedURLCache(NSURLCacheClass)
  If sharedCache <> Nil Then
    removeAllCachedResponses(sharedCache)
  Else
    //MsgBox("Failed to get shared URL cache.")
  End If
Else
  //MsgBox("Failed to get NSURLCache class.")
End If

Not exactly what you are looking for but this swift code seem to do it.

@Christian_Schmitz might have a solution in his plug-ins?

Of course we have NSHTTPCookieStorageMBS class.

Sounds good. I tried this but still can access Facebook account or other logged website. My acccess should have been reset?

Dim cookieStorage As NSHTTPCookieStorageMBS = NSHTTPCookieStorageMBS.sharedHTTPCookieStorage

// Get all cookies
Dim cookies() As NSHTTPCookieMBS = cookieStorage.cookies

// Loop through all cookies and delete them
For Each cookie As NSHTTPCookieMBS In cookies
  cookieStorage.deleteCookie(cookie)
Next

@Christian_Schmitz any update on that? Really need an effective way to reset cache and cookies before to launch an URL. So like that, people login again.

Wait, I just noticed, that we have WKHTTPCookieStoreMBS for newer WebKit 2 based HTMLViewer:

Var CookieStore As WKHTTPCookieStoreMBS = WKWebViewControlMBS1.WKWebView.HTTPCookieStore
Var cookies() As NSHTTPCookieMBS = CookieStore.AllCookies
Var cookie As NSHTTPCookieMBS = cookies(0)

Also you may just want to use private mode. See UsePrivateBrowsing property for WKWebViewControlMBS control.

1 Like

Btw, ChatGPT even the new o1 version keep telling about a method named WKWebsiteDataStoreMBS that doesn’t seems to exist anymore :wink:

// Use MBS Plugin classes

// Get the default data store
Dim dataStore As WKWebsiteDataStoreMBS = WKWebsiteDataStoreMBS.defaultDataStore

// Specify the data types to remove
Dim dataTypes As Set = WKWebsiteDataStoreMBS.allWebsiteDataTypes

// Specify the date from which to remove data (e.g., from the epoch to remove all data)
Dim fromDate As New Date(1970, 1, 1, 0, 0, 0)

// Remove the data with a completion handler
dataStore.removeDataOfTypes(dataTypes, fromDate) do
  // Data removal is complete
  MessageBox("Cookies and cache cleared.")
End

Well, we could of course add a WKWebsiteDataStore wrapper as WKWebsiteDataStoreMBS to the plugins in the future if that would be useful.