【问题标题】:How do you set localstorage so it persist across program runs你如何设置 localstorage 以便它在程序运行中持续存在
【发布时间】:2011-12-21 18:50:39
【问题描述】:

我有一个本地存储,用于在程序中运行 web 视图,但它总是在下一个程序运行开始时被清除。我检查了文件,它在程序运行后确实存在,但在下一个程序开始时,它会清除它。

这是我的代码:

- (void)awakeFromNib {
    WebPreferences *prefs = [webView preferences];
    [prefs _setLocalStorageDatabasePath:@"~/Library/Application Support/MyApp"];
    [prefs setDatabasesEnabled:YES];
    [prefs setLocalStorageEnabled:YES];

    NSString *resourcesPath = [[NSBundle mainBundle] resourcePath];
    NSString *htmlPath = [resourcesPath stringByAppendingString:@"/htdocs/index.html"];
    [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]];

    [window setDelegate:self];
}

任何帮助将不胜感激!谢谢

【问题讨论】:

标签: cocoa webview local-storage


【解决方案1】:

查看有关 WebPreferences 的文档,您每次都使用您的 webview 初始化它,WebPreferences 是 WebView 之前的,这导致您每次启动应用程序时都会创建一个新的 WebPreferences。如果你想保持它的持久性,你应该用 initWithIdentifier:"indentifer" 来初始化它。

WebPreferences Class Reference

【讨论】:

  • 这似乎是原因,但是如果您没有获得 webview 的偏好对象,您如何设置偏好?
  • 仔细阅读文档,在 initWithIdentifier 下,通过向 WebView 对象发送 setPreferencesIdentifier: 消息来设置首选项标识符。
  • 这种方式好像不影响擦除,我试了下代码:pastie.org/3122277
【解决方案2】:

我相信您知道使用私有 API 的风险。

也就是说,在那里放置一些断点,看看缓存在什么时候被吹走。尝试将数据库移动到该行之前的临时位置,然后立即将其放回原处。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多