我在程序中拥有用于 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"来初始化它。
关于cocoa - 如何设置本地存储以便它在程序运行期间持续存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8594822/