Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

localStorage not persisting in OSX app (Xcode 4.3)

From what I have seen, if you are building a OSX desktop HTML5 app and want localStorage to persist in your WebView wrapper, you need to do something like this:

WebPreferences* prefs = [webView preferences];
[prefs _setLocalStorageDatabasePath:@"~/Library/Application Support/MyApp"];
[prefs setLocalStorageEnabled:YES];

Taken from: How do I enable Local Storage in my WebKit-based application?

But this doesn't seem to work for me in Xcode 4.3. Instead I get

"No visible @interface for 'WebPreferences' declares the selector '_setLocalStorageDatabasePath:'
"No visible @interface for 'WebPreferences' declares the selector 'setLocalStorageEnabled:'

I'm very new to Objective C, and are probably doing something silly like not including some header or something.

I've included the WebKit framework and both of these headers:

#import <WebKit/WebKit.h>
#import <WebKit/WebPreferences.h>

And what's weird is that I can access other methods of prefs, i.e. [prefs setDefaultFontSize:10] - but just not the two above that I listed.

Any ideas? Is this something that has been removed in Xcode 4.3?

like image 945
asgeo1 Avatar asked May 15 '12 22:05

asgeo1


1 Answers

OK, I have a solution. I looked at the source code to macgap and noticed how they were dealing with this issue.

It turns out the error message I was getting does make a little sense - I needed to declare an interface for WebPreferences first.

@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];

Like I said, I'm new to Objective-C. I don't really get why the interface is needed in order to call those two methods (i.e. when I can call the other methods without the interface).

like image 113
asgeo1 Avatar answered Oct 17 '22 11:10

asgeo1