use*_*856 6 cocoa objective-c webview local-storage
我正在尝试让本地存储在Cocoa中的WebView中工作.我在另一个SO问题中使用了此处显示的代码,但它对我来说无法正常工作.正确创建本地存储并使其内容保持重新加载,但无论何时重新启动应用程序,都会立即删除旧的本地存储.
例如,我创建了一个新项目并在窗口中设置了一个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"];
}
Run Code Online (Sandbox Code Playgroud)
本地存储正确存储在正确的文件夹中,即使在退出应用程序后也会保留在那里,但是当应用程序再次启动时,旧的本地存储将被删除并创建一个新文件.
在经历了很多痛苦和挫折之后,我找到了一种启用本地存储的方法,并让它在应用程序运行中保持正常运行.此解决方案专门针对OSX,但也可能适用于iOS.
下载此头文件并将其添加到项目中.它不包含在XCode Webkit发行版中.
单击下载WebStorageManagerPrivate.h
添加到它,以下行:
static NSString* _storageDirectoryPath();
+ (NSString *)_storageDirectoryPath;
Run Code Online (Sandbox Code Playgroud)
这些允许您检索WebKit本地存储跟踪器数据库的目录位置.这很重要,因为由于WebKit中的错误,如果您不将LocalStorage WebView文件存储在跟踪器数据库所在的目录中,则每次运行应用程序时都会删除它们.我没有在WebStorageManager代码中看到为单个应用程序更改此位置的方法.始终从用户首选项中读取.
在appDelegate中包含WebStorageManagerPrivate.h.
#include "WebStorageManagerPrivate.h"
Run Code Online (Sandbox Code Playgroud)
您需要下载并在项目中包含另一个未包含在XCode发行版中的标头.将其另存为WebPreferencesPrivate.h
在appDelegate中包含WebPreferencesPrivate.h.
#include "WebPreferencesPrivate.h"
Run Code Online (Sandbox Code Playgroud)
现在使用applicationDidFinishLaunching处理程序中的以下代码初始化并启用LocalStorage.该代码假定您有一个名为"webView"的IBOutlet用于您正在使用的WebView.
NSString* dbPath = [WebStorageManager _storageDirectoryPath];
WebPreferences* prefs = [self.webView preferences];
NSString* localDBPath = [prefs _localStorageDatabasePath];
// PATHS MUST MATCH!!!! otherwise localstorage file is erased when starting program
if( [localDBPath isEqualToString:dbPath] == NO) {
[prefs setAutosaves:YES]; //SET PREFS AUTOSAVE FIRST otherwise settings aren't saved.
// Define application cache quota
static const unsigned long long defaultTotalQuota = 10 * 1024 * 1024; // 10MB
static const unsigned long long defaultOriginQuota = 5 * 1024 * 1024; // 5MB
[prefs setApplicationCacheTotalQuota:defaultTotalQuota];
[prefs setApplicationCacheDefaultOriginQuota:defaultOriginQuota];
[prefs setWebGLEnabled:YES];
[prefs setOfflineWebApplicationCacheEnabled:YES];
[prefs setDatabasesEnabled:YES];
[prefs setDeveloperExtrasEnabled:[[NSUserDefaults standardUserDefaults] boolForKey: @"developer"]];
#ifdef DEBUG
[prefs setDeveloperExtrasEnabled:YES];
#endif
[prefs _setLocalStorageDatabasePath:dbPath];
[prefs setLocalStorageEnabled:YES];
[self.webView setPreferences:prefs];
}
Run Code Online (Sandbox Code Playgroud)
我希望这有助于其他人一直在努力或仍在努力解决这个问题,直到它在WebKit中正确修复.
WebView在OS X上目前不支持localStorage.要求此功能的最佳方法是提交错误https://developer.apple.com/bugreporter/并提及它是#11026838的副本.
您必须使用Cocoa API存储数据,以便在应用程序启动时保留它.
对于像数据这样简单的"偏好",NSUserDefaults是最好的解决方案.它是一个简单的键/值存储,类似于localStorage提供的.
有关更复杂的数据,您可能需要查看使用NSKeyedArchiver和NSKeyedUnarchiver,请参阅" 存档和序列化编程指南".
对于极其复杂或高性能的数据,您可以使用Core Data.
有关互操作Objective-C和JavaScript的更多信息,请参阅从JavaScript调用Objective-C方法.
| 归档时间: |
|
| 查看次数: |
3053 次 |
| 最近记录: |