Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access HTML5 local storage created by PhoneGap on iOS?

We are transitioning away from PhoneGap to a truly native iPhone app and we need to access user data stored in HTML5 local storage created by PhoneGap. How do we get at that data so that we can create a seamless update process for the user?

like image 293
Steve Moser Avatar asked Jan 30 '12 16:01

Steve Moser


3 Answers

I haven't tried Mattias' way but I just accessed the webkit local storage database directly and then imported the sqlite3 lib included with the iPhone SDK to extract the values I needed. This is how you access localstorage created by PhoneGap:

NSString *databaseName = @"file__0.localstorage"; 

//Get Library path 
NSArray *libraryPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, 
                                                            NSUserDomainMask, YES); 
NSString *libraryDir = [libraryPaths objectAtIndex:0];

NSString *databasePath = [libraryDir 
                             stringByAppendingPathComponent:@"WebKit/LocalStorage/"]; 

NSString *databaseFile = [databasePath 
                             stringByAppendingPathComponent:databaseName]; 

BOOL webkitDb; 

NSFileManager *fileManager = [NSFileManager defaultManager]; 

webkitDb = [fileManager fileExistsAtPath:databaseFile]; 

if (webkitDb) {
    MMWebKitLocalStorageController* wkc = [[MMWebKitLocalStorageController alloc] init];
    [wkc updateUserFromLocalStorage:databaseFile];
}
like image 132
Steve Moser Avatar answered Oct 29 '22 22:10

Steve Moser


I don't think there is currently any native API for accessing the local storage but you could try to create a web view with some JavaScript that dumps the local storage to i.e. JSON.

Create a UIWebView that has a UIWebViewDelegate delegate that implements the webViewDidFinishLoad: method and uses the stringByEvaluatingJavaScriptFromString: method to trigger a dump.

Something like this, first the page to load in the web view:

<html>
<head>
  <script>
  // download and insert JSON-js here
  // https://github.com/douglascrockford/JSON-js

  function dumpLocalStorageToJSON() {
    d = {};
    for(i = 0; i < localStorage.length; i++)
      d[localStorage.key(i)] = localStorage.getItem(localStorage.key(i));
    return JSON.stringify(d);
  }
  </script>
</head>
</html>

Then the finish load delegate method:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
  NSString *localStorageAsJSON = [webView stringByEvaluatingJavaScriptFromString:@"dumpLocalStorageToJSON();"];
  // parse JSON and do something useful
}
like image 40
Mattias Wadman Avatar answered Oct 30 '22 00:10

Mattias Wadman


I just provide some detail FYI,the LocalStorage save in the database whitch 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,convert to NSString with code:

[[NSString alloc] initWithData:data encoding:NSUTF16LittleEndianStringEncoding];
like image 41
CR.MO Avatar answered Oct 29 '22 23:10

CR.MO