【问题标题】:How clear localStorage for UIWebViews in iOSiOS中UIWebViews的localStorage如何清晰
【发布时间】:2023-04-06 22:21:01
【问题描述】:

我现在的代码删除了*.localstorage 和备份的localStorage 文件,但是无论文件是否被删除,下次打开web 视图时localStorage 仍然存在。

编辑:我必须删除所有域的 localStorage! IE。注入 JS 不起作用。

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

【问题讨论】:

    标签: ios swift cocoa-touch uiwebview local-storage


    【解决方案1】:

    我现在发现问题中的代码确实有效,一直在手机上,有时在 Xcode iOS 模拟器中。我很确定这与每次重建时都会创建新的应用程序目录这一事实有关,但 localStorage 文件“留在后面”。我创建了一个新问题来回答该问题,并将其标记为已解决,因为问题不是代码:iOS simulator only uses the first app folder for localStorage files but creates a new app folder for everything else

    【讨论】:

      【解决方案2】:

      你可以尝试在你的UIWebView中执行这个javascript

      webView.stringByEvaluatingJavaScriptFromString("localStorage.clear();")
      

      【讨论】:

      • 这种方法的问题在于,它只删除了一个域的本地存储,但是当我使用 openid connect 时,我可能会被重定向到许多不同的站点,这些站点可能会或可能不会将身份验证数据存储在 localstorage 中: (
      【解决方案3】:

      你可以使用

      NSURLCache.sharedURLCache().removeAllCachedResponses()
      NSURLCache.sharedURLCache().diskCapacity = 0
      NSURLCache.sharedURLCache().memoryCapacity = 0
      

      你也可以改变 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)
      

      有关缓存策略的更多信息:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/index.html#//apple_ref/c/tdef/NSURLRequestCachePolicy

      【讨论】:

      • 下次我使用 UIWebView 时 localStorage 不为空 :-(
      • 缓存没有清空本地存储
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-27
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多