简体   繁体   中英

Restore localStorage data from old cordova app

We are porting an application from Cordova to Xamarin.forms. Now it's vital for us to restore the data the old version is keeping in localStorage.

I've attempted to create a WebView in Xamarin and then display the localStorage field I needed, but it seems to be completely empty. However, if I install the old cordova app back, it can still access the data, so it is definitely stored and survives installing the Xamarin version.

The Cordova documentation mentions something about having it's own localStorage implementation on some platforms, but is not exactly clear on which and how.

Basically I'm looking for a way to reliably restore the localStorage data on both android and iOS.

I can currently think of two directions:

  • embed cordova in the xamarin.forms app
  • extract the part of cordova that is doing the local storage and try to make it work with a plain webview

For iOS I ended up figuring out that the local storage data was actually still in Library/WebKit/LocalStorage/file__0.localstorage. But for some reason Xamarin's webview doesn't make the data available. Probably with some fiddling you could get it to (I assume it is related to differences in base url).

As I need just the data, I've opened the localstorage file directly with sqlite.NET and got the data in that way. (Note: it's stored as a blob of Unicode, so not UTF-8 like you would expect)

On Android things worked pretty similar, but in some cases data was in UTF-8, so some detection was required. (In my case: converting it both ways and checking if the resulting value looked a bit like valid JSON)

Some detail FYI on iOS, the LocalStorage save in the database which path is

  1. Library/WebKit/LocalStorage/ (UIWebview)
  2. Library/Caches/ (UIWebview)
  3. Library/WebKit/WebsiteData/LocalStorage/ (WKWebView)

Then open the database,execute query SELECT key,value FROM ItemTable can get key and value .The key is NSString,value is NSData,encode with NSUTF16LittleEndianStringEncoding ( UTF-16LE on Android),convert to NSString with code:

[[NSString alloc] initWithData:data encoding:NSUTF16LittleEndianStringEncoding];

It cant be done. Different app different metadata. It's impossible to do that. It based on your app id for each localstorage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM