MGJ的地盘

爱拼才会赢!

OSX中Webview设置localStorage缓存 231

作者为 发表

mac osx 的webkit并没有公开的api可以定义设置缓存
会导致关闭程序后缓存就没有了,下次进应用,webview取不到存储的localStorage

网上见到如下方法可以解决

WebPreferences *webPrefs = [self.webView preferences];
[webPrefs _setLocalStorageDatabasePath:savePath];
[webPrefs setLocalStorageEnabled:YES];

在Xcode4.3以后也就是OSX 10.7 的SDK就不能使用该方法
可以用分类的方式调用到私有的api,经测试在OSX 10.8也是适用的

@interface WebPreferences (WebPreferencesPrivate)
- (void)_setLocalStorageDatabasePath:(NSString *)path;
- (void) setLocalStorageEnabled: (BOOL) localStorageEnabled;
- (void) setDatabasesEnabled:(BOOL)databasesEnabled;
- (void) setDeveloperExtrasEnabled:(BOOL)developerExtrasEnabled;
- (void) setWebGLEnabled:(BOOL)webGLEnabled;
- (void) setOfflineWebApplicationCacheEnabled:(BOOL)offlineWebApplicationCacheEnabled;
@end

然后就可以调用到WebPreferencesPrivate的私有方法了

WebPreferences *webPrefs = [WebPreferences standardPreferences];

NSString *cappBundleName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
NSString *applicationSupportFile = [@"~/Library/Application Support/" stringByExpandingTildeInPath];
NSString *savePath = [NSString pathWithComponents:[NSArray arrayWithObjects:applicationSupportFile, cappBundleName, @"LocalStorage", nil]];
[webPrefs _setLocalStorageDatabasePath:savePath];
[webPrefs setLocalStorageEnabled:YES];
[webPrefs setDatabasesEnabled:YES];
[webPrefs setDeveloperExtrasEnabled:[[NSUserDefaults standardUserDefaults] boolForKey: @"developer"]];
[webPrefs setOfflineWebApplicationCacheEnabled:YES];
[webPrefs setWebGLEnabled:YES];

[self.webView setPreferences:webPrefs];


评论功能已被关闭。


 主题设计 • skyfrit.com  MGJ的地盘 | 保留所有权利

50 queries in 0.589 seconds.