WebView中的本地存储不是持久的
我正在尝试让本地存储在 Cocoa 的 WebView 中工作。我使用了代码在另一个SO问题中显示< /a>,但它对我来说不能正常工作。本地存储已正确创建,并在重新加载时保留其内容,但每当应用程序重新启动时,旧的本地存储将立即删除。
例如,我创建了一个新项目并在窗口内设置了一个 WebView。然后,我将以下代码放入我的 AppDelegate.m
中:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
WebPreferences *prefs = [webView preferences];
[prefs _setLocalStorageDatabasePath:@"~/Library/Application Support/Test"];
[prefs setLocalStorageEnabled:YES];
[webView setMainFrameURL:@"http://static.diveintojavascript.com/files/tutorials/web-storage-contacts/contacts.html"];
}
本地存储正确存储在正确的文件夹中,即使在退出应用程序后仍保留在那里,但是当应用程序再次启动时,旧的本地存储会丢失。删除并创建一个新文件。
I'm trying to get local storage to work in a WebView in Cocoa. I used code shown here in another SO question, but it doesn't work properly for me. The local storage is created properly and keeps its contents across reloads, but whenever the application is restarted, the old local storage is immediately deleted.
For example, I created a new project and set up a WebView inside the window. I then put the following code in my AppDelegate.m
:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
WebPreferences *prefs = [webView preferences];
[prefs _setLocalStorageDatabasePath:@"~/Library/Application Support/Test"];
[prefs setLocalStorageEnabled:YES];
[webView setMainFrameURL:@"http://static.diveintojavascript.com/files/tutorials/web-storage-contacts/contacts.html"];
}
The local storage is stored properly in the correct folder and stays there even after quitting the app, but when the app is started again the old local storage is deleted and a new file is created.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过大量的痛苦和挫折后,我找到了一种启用本地存储并使其在应用程序正常运行时持续存在的方法。该解决方案专门针对 OSX,但也可能适用于 iOS。
下载此头文件并将其添加到您的项目中。它不包含在 XCode Webkit 发行版中。
点击下载WebStorageManagerPrivate.h
添加到其中,以下几行:
这些行允许您检索 WebKit 本地存储跟踪器数据库的目录位置。这很重要,因为由于 WebKit 中的错误,如果您不将 LocalStorage WebView 文件存储在与跟踪器数据库相同的目录中,那么每次运行应用程序时它们都会被删除。我没有在 WebStorageManager 代码中看到更改单个应用程序的此位置的方法。它始终从用户偏好中读取。
将 WebStorageManagerPrivate.h 包含在您的 appDelegate 中。
您需要下载 XCode 发行版中未包含的另一个标头并将其包含在项目中。将其另存为 WebPreferencesPrivate.h
点击下载WebPreferencesPrivate.h
将 WebPreferencesPrivate.h 包含在您的 appDelegate 中。
现在,在 applicationDidFinishLaunching 处理程序中使用以下代码来初始化并启用 LocalStorage。该代码假设您正在使用的 WebView 有一个名为“webView”的 IBOutlet。
我希望这可以帮助其他已经或仍在解决这个问题的人,直到它在 WebKit 中得到正确解决。
After a lot of pain and frustration I found a way to enable local storage and have it persist across application runs properly. This solution is specifically for OSX, but it may be applicable to iOS as well.
Download and add this header file into your project. It's not included in the XCode Webkit distribution.
click to download WebStorageManagerPrivate.h
Add to it, the following lines:
These allow you to retrieve the directory location of the WebKit local storage tracker database. This is important because due to a bug in WebKit, if you don't store your LocalStorage WebView files in the same directory as the tracker database, they are deleted every other time you run your application. I didn't see a way in the WebStorageManager code to change this location for an individual application. It is always read from the user preferences.
Include the WebStorageManagerPrivate.h in your appDelegate.
You need to download and include in your project another header not included in XCode distribution. Save it as WebPreferencesPrivate.h
click to download WebPreferencesPrivate.h
Include the WebPreferencesPrivate.h in your appDelegate.
Now use the code below in your applicationDidFinishLaunching handler to initialize and enable LocalStorage. The code assumes you have an IBOutlet named 'webView' for the WebView you are using.
I hope this helps others have struggled or are still struggling with this issue, until it is fixed properly within WebKit.
OS X 上的
WebView
目前不支持localStorage
。请求此功能的最佳方式是在 https://developer.apple.com/bugreporter/ 并提及它与 #11026838 重复。您必须使用 Cocoa API 来存储数据,以便在应用程序启动时保留数据。
对于像数据这样的简单“偏好”, NSUserDefaults 是最好的解决方案。它是一个简单的键/值存储,类似于 localStorage 提供的功能。
对于更复杂的数据,您可能需要使用
NSKeyedArchiver
和NSKeyedUnarchiver
,请参阅 存档和序列化编程指南。对于极其复杂或高性能的数据,您可以使用核心数据。
有关互操作 Objective-C 和 JavaScript 的更多信息,请参阅 从 JavaScript 调用 Objective-C 方法。
WebView
on OS X does not supportlocalStorage
at this time. The best way to ask for this feature is to file a bug at https://developer.apple.com/bugreporter/ and mention that it is a duplicate of #11026838.You will have to store your data using Cocoa APIs to persist it across application launches.
For simple "preference" like data, NSUserDefaults is the best solution. It is a simple key/value store, similar to what localStorage offers.
For more complex data, you may want to look at using
NSKeyedArchiver
andNSKeyedUnarchiver
, see the Archives and Serializations Programming Guide.For extremely complex or high-performance data, you could use Core Data.
For more information on interoperating Objective-C and JavaScript, see Calling Objective-C Methods From JavaScript.