据我所知,如果您正在构建 OSX 桌面 HTML5 应用程序并希望 localStorage 保留在您的 WebView 包装器中,您需要执行如下操作:
WebPreferences* prefs = [webView preferences];
[prefs _setLocalStorageDatabasePath:@"~/Library/Application Support/MyApp"];
[prefs setLocalStorageEnabled:YES];
取自:How do I enable Local Storage in my WebKit-based application?
但这在 Xcode 4.3 中似乎对我不起作用。相反,我得到了
"No visible @interface for 'WebPreferences' declares the selector '_setLocalStorageDatabasePath:'
"No visible @interface for 'WebPreferences' declares the selector 'setLocalStorageEnabled:'
我是 Objective C 的新手,可能正在做一些愚蠢的事情,比如不包含一些 header 或其他东西。
我已经包含了 WebKit 框架和这两个 header :
#import <WebKit/WebKit.h>
#import <WebKit/WebPreferences.h>
奇怪的是我可以访问 prefs 的其他方法,即 [prefs setDefaultFontSize:10]
- 但我上面列出的两个不行。
有什么想法吗?这是 Xcode 4.3 中删除的东西吗?
最佳答案
好的,我有办法。我查看了 macgap 的源代码并注意到他们是如何处理这个问题的。
事实证明,我收到的错误消息确实有点道理 - 我需要先为 WebPreferences 声明一个接口(interface)。
@interface WebPreferences (WebPreferencesPrivate)
- (void)_setLocalStorageDatabasePath:(NSString *)path;
- (void) setLocalStorageEnabled: (BOOL) localStorageEnabled;
@end
...
WebPreferences* prefs = [WebPreferences standardPreferences];
[prefs _setLocalStorageDatabasePath:"~/Library/Application Support/MyApp"];
[prefs setLocalStorageEnabled:YES];
[webView setPreferences:prefs];
正如我所说,我是 Objective-C 的新手。我真的不明白为什么需要接口(interface)才能调用这两种方法(即什么时候我可以在没有接口(interface)的情况下调用其他方法)。
关于objective-c - localStorage 不存在于 OSX 应用程序中 (Xcode 4.3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10609644/