cocoa - 如何设置本地存储以便它在程序运行期间持续存在

标签 cocoa webview local-storage

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

这是我的代码:

- (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];
}

如有任何帮助,我们将不胜感激!谢谢

最佳答案

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

WebPreferences Class Reference

关于cocoa - 如何设置本地存储以便它在程序运行期间持续存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8594822/

相关文章:

ios - 无法在 Swift 3 中启动 Activity Indicator?

javascript - 在状态更改时 react native WebView 重新加载

javascript - 检查 localStorage.removeItem() 是否成功

javascript - 删除用户输入时出现问题

Javascript 从另一个脚本结果中提取或映射特定的 id/值

objective-c - 打印离屏 PDFView

python - 从 Objective-C 调用 Python

objective-c - 类别中的私有(private)方法,使用匿名类别

objective-c - 通过键盘快捷键的 xcode 3.2.1 文件历史记录

android - WebView 中的 TalkBack 辅助功能