ios - 如何清除 iOS 中 UIWebViews 的 localStorage

标签 ios swift cocoa-touch uiwebview local-storage

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

编辑:我必须删除所有域的 localStorage! IE。注入(inject) 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)
}

最佳答案

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

关于ios - 如何清除 iOS 中 UIWebViews 的 localStorage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30217167/

相关文章:

ios - 是否可以结束 SKAction Action 中途?

swift - 容器 View 中的 definesPresentationContext = true 仍然允许在背景 View 上使用模态动画

iphone - iOS7 中 UITableView 滚动指示器闪烁

iphone - 按顺序播放多媒体内容

ios - 旋转变换后调整 UIView 的大小

ios - 在新窗口或选项卡中打开的链接未在 native iOS 中加载

ios - iphone-如何在 ios 中不导航消息窗口的情况下发送 SMS [未重复]

ios - 关于 ios objective-c 编程中委托(delegate)的困惑

ios - 在遍历核心数据对象数组时访问日期类型属性随机失败

objective-c - Downpicker 没有出现在 UITableView 中