SHARE
TWEET

NATIVE Persistent LocalStorage Phonegap Support

photokandy Mar 8th, 2012 12,518 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *
  3.  * PERSISTENT LOCALSTORAGE NATIVE SOLUTION for phonegap (tested on 1.3)
  4.  * iOS 5.1 puts LocalStorage in a vulnerable location (Libary/Caches). This will copy it to
  5.  * the documents folder as "appdata.db" so that it will be appropriately backed-up and not
  6.  * overwritten. If "appdata.db" doesn't exist, localStorage will not be overwritten.
  7.  * Also, upon application suspend (resignActive) or termination, localStorage is saved, so
  8.  * there should not be any chance that changes to localStorage won't be persisted. [[ short of a power-cycle ]]
  9.  *
  10.  * The following should be placed in AppDelegate.m. Note that it //partially// replaces
  11.  * webViewDidStartLoad:. The remainder of webViewDidStartLoad: (as in phonegap 1.3) is below,
  12.  * replace/remove as appropriate
  13.  *
  14.  * @Author: Kerri Shotts (2012)
  15.  * @License: MIT License
  16.  *
  17.  * Notes: USES ARC. IF USING MRC, apply the appropriate releases.
  18.  *
  19.  * Based partially on http://gauravstomar.blogspot.com/2011/08/prepopulate-sqlite-in-phonegap.html
  20.  *
  21.  */
  22. - (BOOL)fileExists: (NSString*) theFile
  23. {
  24.     NSFileManager *fileManager = [NSFileManager defaultManager];
  25.     return [fileManager fileExistsAtPath:theFile];
  26.     // MRC: make sure to release
  27. }
  28.  
  29. - (void)copyFile:(NSString*) sourceFile to:(NSString*) targetPath withName:(NSString*) targetFile
  30. {
  31.     NSFileManager *fileManager = [NSFileManager defaultManager];
  32.     [fileManager createDirectoryAtPath:targetPath withIntermediateDirectories:YES attributes:nil error:NULL];
  33.     NSString *fullTargetFile = [targetPath stringByAppendingPathComponent:targetFile];
  34.    
  35.     NSLog(@"Source File for Copy: %@", sourceFile);
  36.     NSLog(@"Target File for Copy: %@", fullTargetFile);
  37.    
  38.     if ( [self fileExists:fullTargetFile] )
  39.     {
  40.         // remove the file first. (Ick! I wish there was a better way...
  41.         if ( [fileManager removeItemAtPath:fullTargetFile error:nil] == YES )
  42.         {
  43.             NSLog (@"Target successfully removed.");
  44.         }
  45.         else
  46.         {
  47.             NSLog (@"Target could not be removed prior to copy. No copy will occur.");
  48.             return;
  49.         }
  50.     }
  51.    
  52.     if ( [fileManager copyItemAtPath:sourceFile toPath:fullTargetFile error:nil] == YES)
  53.     {
  54.         NSLog(@"Copy successful.");
  55.     }
  56.     else
  57.     {
  58.         NSLog(@"Copy unsuccessful.");
  59.     }
  60.     // MRC: don't forget to release fileManager where necessary!
  61. }
  62.  
  63.  
  64. - (BOOL)isIOS5_1OrHigher
  65. {
  66.     // based on: http://stackoverflow.com/a/9320041
  67.     NSArray *versionCompatibility = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
  68.  
  69.     if ( [[versionCompatibility objectAtIndex:0] intValue] > 5 )
  70.     {
  71.         return YES; // iOS 6+
  72.     }
  73.    
  74.     if ( [[versionCompatibility objectAtIndex:0] intValue] < 5 )
  75.     {
  76.         return NO;  // iOS 4.x or lower
  77.     }
  78.    
  79.     if ( [[versionCompatibility objectAtIndex:1] intValue] >= 1 )
  80.     {
  81.         return YES; // ios 5.<<1>> or higher
  82.     }
  83.    
  84.     return NO;  // ios 5.<<0.x>> or lower
  85.  
  86. }
  87.  
  88. - (void)copyPersistentStorageToLocalStorage
  89. {
  90.     // build localStorage path: ~/Library/WebKit/LocalStorage/file__0.localstorage (for iOS < 5.1)
  91.     //                          ~/Library/Caches/file__0.localstorage (for iOS >= 5.1 )
  92.     NSString *localStoragePath;
  93.     if ( [self isIOS5_1OrHigher] )
  94.     {
  95.         // for IOS >= 5.1
  96.         localStoragePath =
  97.                           [
  98.                            [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]
  99.                            stringByAppendingPathComponent:@"Caches"
  100.                           ];
  101.     }
  102.     else
  103.     {
  104.         // for IOS < 5.1;
  105.         localStoragePath =
  106.                           [
  107.                            [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]
  108.                            stringByAppendingPathComponent:@"WebKit/LocalStorage"
  109.                           ];
  110.     }
  111.    
  112.     // build persistentStorage path: ~/Documents/appdata.db
  113.     NSString *persistentStoragePath =
  114.                                    [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  115.     NSString *persistentStorageFile = [persistentStoragePath stringByAppendingPathComponent:@"appdata.db"];
  116.    
  117.     // does the persistent store exist?
  118.     if ([self fileExists:persistentStorageFile ])
  119.     {
  120.         // it does, copy it over localStorage
  121.         NSLog(@"Copying persistent storage to local storage.");
  122.         [self copyFile:persistentStorageFile to:localStoragePath withName: @"file__0.localstorage"];
  123.     }
  124.     else
  125.     {
  126.         NSLog(@"No persistent storage to copy. Using local storage only.");
  127.     }
  128. }
  129.  
  130. - (void)copyLocalStorageToPersistentStorage
  131. {
  132.     // build localStorage path: ~/Library/WebKit/LocalStorage/file__0.localstorage (for iOS < 5.1)
  133.     //                          ~/Library/Caches/file__0.localstorage (for iOS >= 5.1 )
  134.     NSString *localStoragePath;
  135.     if ( [self isIOS5_1OrHigher] )
  136.     {
  137.         // for IOS >= 5.1
  138.         localStoragePath =
  139.                           [
  140.                            [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]
  141.                            stringByAppendingPathComponent:@"Caches"
  142.                           ];
  143.     }
  144.     else
  145.     {
  146.         // for IOS < 5.1;
  147.         localStoragePath =
  148.                           [
  149.                            [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]
  150.                            stringByAppendingPathComponent:@"WebKit/LocalStorage"
  151.                           ];
  152.     }
  153.  
  154.     NSString *localStorageFile = [localStoragePath stringByAppendingPathComponent:@"file__0.localstorage"];
  155.    
  156.     // build persistentStorage path: ~/Documents/appdata.db
  157.     NSString *persistentStoragePath =
  158.                                    [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  159.  
  160.     // does the local store exist? (it almost always will)
  161.     if ([self fileExists:localStorageFile ])
  162.     {
  163.         // it does, copy it over persistent Storage
  164.         NSLog(@"Copying local storage to persistent storage.");
  165.         [self copyFile:localStorageFile to:persistentStoragePath withName:@"appdata.db"];
  166.     }
  167.     else
  168.     {
  169.         NSLog(@"No local storage to copy. Using local storage only.");
  170.     }
  171.  
  172. }
  173.  
  174. - (void)applicationWillResignActive:(UIApplication *)application
  175. {  
  176.     // move the local storage data to persistent storage
  177.     // while we're resigning so that we know our data is safe...
  178.     [self copyLocalStorageToPersistentStorage];
  179.     return;
  180. }
  181.  
  182. - (void)applicationWillTerminate:(UIApplication *)application
  183. {
  184.     // move the local storage data to persistent storage
  185.     // while we're terminating so that we know our data is safe...
  186.     [self copyLocalStorageToPersistentStorage];
  187.     return;
  188. }
  189.  
  190. - (void)webViewDidStartLoad:(UIWebView *)theWebView
  191. {
  192.     [self copyPersistentStorageToLocalStorage];
  193. //
  194. // END Persistent LocalStorage solution. Remainder of webViewDidStartLoad: should be used.
  195. //
  196.         return [ super webViewDidStartLoad:theWebView ];
  197. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top