Skip to content

Instantly share code, notes, and snippets.

@orbitaloop
Created March 10, 2012 00:50
Show Gist options
  • Save orbitaloop/2009491 to your computer and use it in GitHub Desktop.
Save orbitaloop/2009491 to your computer and use it in GitHub Desktop.
Fix to copy the WebSQL DB to the document Folder (because iOS5.1 use now the Library/Caches folder and doesn't do the migration)
/* Based on the code of ScottP and Gaurav Tomar ( http://gauravstomar.blogspot.com/2011/08/prepopulate-sqlite-in-phonegap.html */
/* Put that code in AppDelegate.m */
- (void)webViewDidStartLoad:(UIWebView *)theWebView
{
NSString *databaseName = @"0000000000000001.db";
//Get Documents path
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databaseFile = [documentsDirectory stringByAppendingPathComponent:databaseName];
NSFileManager *fileManager = [NSFileManager defaultManager];
// If the database in Documents folder already exists
if([fileManager fileExistsAtPath:databaseFile])
{
[fileManager release];
return [ super webViewDidStartLoad:theWebView ];
}
//Get Library path
NSArray *libraryPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDir = [libraryPaths objectAtIndex:0];
//Get <iOS5.1 webkit db path and size
NSString *olddatabasePath = [libraryDir stringByAppendingPathComponent:@"WebKit/Databases/file__0/"];
NSString *olddatabaseFile = [olddatabasePath stringByAppendingPathComponent:databaseName];
NSError *attributesError = nil;
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:olddatabaseFile error:&attributesError];
long long oldDbfileSize = [[fileAttributes objectForKey:NSFileSize] longLongValue];
BOOL oldWebkitDb = [fileManager fileExistsAtPath:olddatabaseFile];
//Get iOS5.1 webkit db path and size
NSString *newdatabasePath = [libraryDir stringByAppendingPathComponent:@"Caches/file__0/"];
NSString *newdatabaseFile = [newdatabasePath stringByAppendingPathComponent:databaseName];
fileAttributes = [fileManager attributesOfItemAtPath:newdatabaseFile error:&attributesError];
long long newDbfileSize = [[fileAttributes objectForKey:NSFileSize] longLongValue];
BOOL newWebkitDb = [fileManager fileExistsAtPath:newdatabaseFile];
//If there is a db file inside WebKit, and that file is bigger than the Caches/dbfile:
if(oldWebkitDb && oldDbfileSize > newDbfileSize)
{
// Need to copy from <App home>Library/Webkit to <app home>/ Documents
// If the old webkit db is there then proceed to copy the database to the users Documents
[fileManager createDirectoryAtPath:newdatabasePath withIntermediateDirectories:YES attributes:nil error:NULL];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:olddatabaseFile
toPath:databaseFile error:nil];
}
else if(newWebkitDb)
{ // Need to copy from <App home>Library/Cache to <app home>/ Documents
// If the new webkit db is there then proceed to copy the database to the users Documents
// Create the database folder structure
[fileManager createDirectoryAtPath:newdatabasePath withIntermediateDirectories:YES attributes:nil error:NULL];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:newdatabaseFile toPath:databaseFile error:nil];
}
// depending on your ARC setting you could uncomment the release
//[fileManager release];
return [ super webViewDidStartLoad:theWebView ];
}
@floodedcodeboy
Copy link

Am i correct in saying that Apple doesn't like this and wont approve the application if this method is used?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment