Reputation: 11816
The code that I have now deletes the *.localstorage
and backup localStorage files, but the localStorage is still persisted the next time the web-view is opened regardless of the fact that the files are deleted.
EDIT: I have to delete the localStorage for ALL domains! I.E. injecting JS won't work.
class func clearWebViewStorage() {
let searchPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).last as! String
let files = NSFileManager.defaultManager().contentsOfDirectoryAtPath(searchPath, error:nil) as! [String]
for file in files {
if file.pathExtension == "localstorage" {
NSLog("Removing localstorage file: %@", searchPath.stringByAppendingPathComponent(file))
NSFileManager.defaultManager().removeItemAtPath(searchPath.stringByAppendingPathComponent(file), error: nil)
}
}
var path = searchPath.stringByAppendingPathComponent("Backups").stringByAppendingPathComponent("localstorage.appdata.db")
NSLog("Removing localstorage backup %@", path)
NSFileManager.defaultManager().removeItemAtPath(path, error: nil)
}
Upvotes: 4
Views: 5782
Reputation: 11816
I have now found out that the code in the question does in fact work, all the time on the phone, sometimes in the Xcode iOS simulator. I'm pretty sure it has something to do with the fact that creates new app directories every time you rebuild, but the localStorage files "stays behind". I have created a new question to answer that problem and I'm marking this as solved since it is not the code that is the problem: iOS simulator only uses the first app folder for localStorage files but creates a new app folder for everything else
Upvotes: 3
Reputation: 53301
You can try executing this javascript inside your UIWebView
webView.stringByEvaluatingJavaScriptFromString("localStorage.clear();")
Upvotes: 2
Reputation: 3132
You can use
NSURLCache.sharedURLCache().removeAllCachedResponses()
NSURLCache.sharedURLCache().diskCapacity = 0
NSURLCache.sharedURLCache().memoryCapacity = 0
You can also change the cache policy of the NSURLRequest
let day_url = NSURL(string: "http://www.domain.com")
let day_url_request = NSURLRequest(URL: day_url,
cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 10.0)
let day_webView = UIWebView()
day_webView.loadRequest(day_url_request)
For more information on cache policies : https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/index.html#//apple_ref/c/tdef/NSURLRequestCachePolicy
Upvotes: 0