Web 视图不保存 HTML5 本地存储设置 [英] Web View Not Saving HTML5 Local Storage Settings
问题描述
我有一个在 Safari 中运行良好的网络应用程序(它使用本地存储并保存设置并恢复它们).
I have a web app that works fine in Safari (it uses local storage and saves the settings and restores them).
我在 Xcode 4.5.2 中创建了一个 Web 视图,用于加载我的 Web 应用程序.我知道默认情况下 web 视图不支持本地存储,所以我添加了代码来启用它,但现在该应用程序根本无法运行.
I created a web view in Xcode 4.5.2 that loads my web app. I know by default web view doesn't support local storage so I added code to enable it but now the app doesn't work at all.
我在 AppDelegate.m 中的代码:
My code in AppDelegate.m:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
WebPreferences *prefs = [WebView preferences];.
[prefs _setLocalStorageDatabasePath:@"~/Library/TestApp/LocalStorage"];
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"];
NSURL* fileURL = [NSURL fileURLWithPath:path];
NSURLRequest* request = [NSURLRequest requestWithURL:fileURL];
[self.webView.mainFrame loadRequest:request];
}
@end
这部分是我为了启用本地存储而添加的:
This part is what I added to enable local storage:
WebPreferences *prefs = [WebView preferences];.
[prefs _setLocalStorageDatabasePath:@"~/Library/TestApp/LocalStorage"];
我收到以下错误:预期的表达式"-选择器‘首选项’没有已知的类方法"
I get the following error: "Expected expression" - "No known class method for selector 'preferences'"
推荐答案
-preferences
是 WebView
上的实例方法,而不是类方法.您需要执行 WebPreferences *prefs = [self.webView preference]
来检索您的 WebView
的首选项.除了调用-[WebPreferences _setLocalStorageDatabasePath:]
,我相信你还会想调用-[WebPreferences setLocalStorageEnabled:]
来确保本地存储被启用.
-preferences
is an instance method on WebView
, not a class method. You'll want to do WebPreferences *prefs = [self.webView preferences]
to retrieve the preferences for your WebView
. In addition to calling -[WebPreferences _setLocalStorageDatabasePath:]
, I believe that you'll also want to call -[WebPreferences setLocalStorageEnabled:]
to ensure that local storage is enabled.
这篇关于Web 视图不保存 HTML5 本地存储设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!