2012-01-30 75 views

回答

4

我还没有尝试过的Mattias'的方式,但我只是直接访问webkit本地存储数据库,然后导入iPhone SDK附带的sqlite3库以提取我需要的值。这是您访问由PhoneGap创建的localstorage的方式:

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]; 
} 
+0

不错。你知道这是否由苹果公司记录吗? – 2012-02-01 00:13:33

+0

没有。我从iExplorer中打开PhoneGap应用程序并在myApp/WebKit/LocalStorage/file__0.localstorage中找到sqlite文件,我将更新我的答案以包含文件名。 – 2012-02-01 15:02:27

+1

仅供参考,但5.1,localStorage的位置更改为在Library/Caches下生存。 – 2012-03-09 03:46:41

3

我不认为目前有任何本地API用于访问本地存储,但您可以尝试使用一些将本地存储转储为JSON的JavaScript创建Web视图。

创建UIWebView具有UIWebViewDelegate委托,它实现了webViewDidFinishLoad:方法和使用方法stringByEvaluatingJavaScriptFromString:触发转储。

事情是这样的,第一页在Web视图加载:

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