3

I have localstorage for a webview in a program working but it's always wiped out at the beginning of the next program run. I checked the file and it does persist after a program run but at the beginning of the next one, it wipes it out.

Here's my code:

- (void)awakeFromNib {
    WebPreferences *prefs = [webView preferences];
    [prefs _setLocalStorageDatabasePath:@"~/Library/Application Support/MyApp"];
    [prefs setDatabasesEnabled:YES];
    [prefs setLocalStorageEnabled:YES];

    NSString *resourcesPath = [[NSBundle mainBundle] resourcePath];
    NSString *htmlPath = [resourcesPath stringByAppendingString:@"/htdocs/index.html"];
    [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]];

    [window setDelegate:self];
}

Any help would be appreciated! Thanks

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
NycCompSci
  • 3,137
  • 2
  • 15
  • 16
  • Oh this is not for iOS.. I should have read your disclaimer below. The following two links might still be of use to you: http://stackoverflow.com/questions/2640724 http://stackoverflow.com/questions/4625484/how-to-get-localstorage-to-work-in-a-webview-in-xcode – abelito Dec 31 '11 at 10:55
  • I checked those two before I posted but could not find useful information. I know that localstorage should be possible since it's just a embedded webkit. – NycCompSci Jan 04 '12 at 03:23
  • This worked for me on 10.7 and Xcode 4.3.3: http://stackoverflow.com/questions/10609644/localstorage-not-persisting-in-osx-app-xcode-4-3 – endemic Jul 04 '12 at 18:39

2 Answers2

0

I'm sure you know the risks of using private APIs.

That said, put some breakpoints in there and see at what point the cache is blown away. Try moving the database to a temp location just before that line, then putting it back immediately afterward.

Blake Caldwell
  • 311
  • 2
  • 8
0

look at the doc on WebPreferences, you init it use your webview every time, the WebPreferences is pre WebView, which cause you create a new WebPreferences every time you bring up the application. If you want to keep it persistent, you should init it with initWithIdentifier:"indentifer".

WebPreferences Class Reference

Allen
  • 6,505
  • 16
  • 19
  • This seem like it could be the reason but how do you set the preference if you don't get the preference object of the webview? – NycCompSci Jan 04 '12 at 02:20
  • read the doc carefully, under initWithIdentifier, you set the preferences identifier by sending a setPreferencesIdentifier: message to your WebView object. – Allen Jan 04 '12 at 02:29
  • This approach does not seem to affect the erasure, I tried the following code: http://pastie.org/3122277 – NycCompSci Jan 04 '12 at 02:41