title: How clear localStorage for UIWebViews in iOS
body: <p>The code that I have now deletes the <code>*.localstorage</code> 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. </p>
  
  <p><strong>EDIT: I have to delete the localStorage for ALL domains! I.E. injecting JS won't work.</strong></p>
  
  
  
  <pre class="lang-c prettyprint-override"><code>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)
  }
  </code></pre>