djyde/WebShell

Preserve local storage

chesleybrown opened this issue · 7 comments

From what I can tell, local storage is not preserved after quitting the WebShell application. This means for applications that use local storage to save information such as authentication tokens, they are lost when exiting the application resulting in the user having to re-login.

Any idea on how to get local storage to be preserved after exiting the application?

djyde commented

It seems that the Cocoa WebView not support localStorage now. I have to find a hack way to implement it.

I was looking at this example "Enabling localStorage": http://www.lostdecadegames.com/completing-your-native-mac-osx-app-built-in-h/

And looks like it might be possible using something like:

var prefs: WebPreferences = webView.preferences()
prefs._setLocalStorageDatabasePath("~/Library/<APP_NAME>/LocalStorage")

But really not sure where this configuration would be set?

djyde commented

There is no method called setLocalStorageDatabasePath. It might be deprecated.

0xWDG commented

@djyde (bug on iOS) http://stackoverflow.com/questions/18964410/localstorage-no-longer-working-in-ios-7-webview

But the problem with the _setLocalStorageDatabasePath is that it is a private property and isn't built to use it.

we have mainWebview.preferences.cacheModel for caching maybe that will help.

@chesleybrown http://www.lostdecadegames.com/completing-your-native-mac-osx-app-built-in-h/
_Note: this may generate the warning "'WebPreferences' may not respond to '-setLocalStorageDatabasePath:'".


Update

http://stackoverflow.com/questions/28174541/how-to-access-ios-private-apis-in-swift

We'll need to access the private API's also to fix #23

0xWDG commented

Update:

Tried (Objective-C Bridging header):

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

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

And then:

mainWebview.preferences._setLocalStorageDatabasePath("~/Library/Application Support/WebShell/db")
mainWebview.preferences.setLocalStorageEnabled(true)
mainWebview.preferences.setDatabasesEnabled(true)
mainWebview.preferences.setOfflineWebApplicationCacheEnabled(true)

And it's not working.

Notes:


i think the best option is that we'll grab the local storage items and put them in a NSUserDefaults? or something like that. (a better option is Core Data) but i don't know how to use it.
I can make a wrapper what copy and insert the local storage, but for images i think it is tricky.
i will look Thursday for a wrapper

0xWDG commented

Update got something working (after reboot).

> localStorage;
< Storage {WebShell: "is Cool", Product: "WebShell", length: 2} = $2
> localStorage.getItem("Product");
< "WebShell" = $3

Now i only need to build a timer, to get them all saved for re-use.

djyde commented

@wdg great job!