How to access the local HTML5 repository created by PhoneGap in iOS?

We are moving from PhoneGap to a truly native iPhone application, and we need to access user data stored in the local HTML5 repository created by PhoneGap. How can we get this data so that we can create a seamless update process for the user?

+5
source share
2 answers

I have not tried using Mattias, but I simply accessed the webkit local storage database directly and then imported the sqlite3 library included in the iPhone SDK to extract the values ​​I needed. Here's how you access the local storage 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];
}
+4
source

, API , - JavaScript, , JSON.

UIWebView, UIWebViewDelegate, webViewDidFinishLoad: stringByEvaluatingJavaScriptFromString: .

- , -:

<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>

:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
  NSString *localStorageAsJSON = [webView stringByEvaluatingJavaScriptFromString:@"dumpLocalStorageToJSON();"];
  // parse JSON and do something useful
}
+3

All Articles