Created
October 29, 2012 19:29
-
-
Save orbitaloop/3975955 to your computer and use it in GitHub Desktop.
LocalStorage/WebSQL migration from iOS 5.0 (or before) to iOS 5.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Valid Storage path : | |
NSString* library = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)objectAtIndex:0]; | |
NSString *WebSQLSubdir = (IsAtLeastiOSVersion(@"5.1")) ? @"Caches" : @"WebKit/Databases"; | |
NSString *WebSQLPath = [library stringByAppendingPathComponent:WebSQLSubdir]; | |
NSString *WebSQLDb = [WebSQLPath stringByAppendingPathComponent:@"file__0"]; | |
NSString *WebSQLDbFile = [WebSQLDb stringByAppendingPathComponent:@"0000000000000001.db"]; | |
NSError *attributesError = nil; | |
NSFileManager* fileManager = [NSFileManager defaultManager]; | |
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:WebSQLDbFile error:&attributesError]; | |
long long validWebSQLDbFileSize = [[fileAttributes objectForKey:NSFileSize] longLongValue]; | |
NSString* documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; | |
//Migration from Webkit->Caches after update to iOS 5.1 | |
err = nil; | |
NSString *oldWebKitSQLPath = [library stringByAppendingPathComponent:@"WebKit/Databases/file__0/0000000000000001.db"]; | |
if (IsAtLeastiOSVersion(@"5.1") && [fileManager fileExistsAtPath:oldWebKitSQLPath]) { | |
NSError *attributesError = nil; | |
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:oldWebKitSQLPath error:&attributesError]; | |
long long previousfileSize = [[fileAttributes objectForKey:NSFileSize] longLongValue]; | |
if(previousfileSize > validWebSQLDbFileSize) | |
{ | |
if (previousfileSize>0) { | |
//keep the smaller db file and rename it (just in case...) | |
NSString *newPath = [WebSQLDbFile stringByAppendingString:@"old.db"]; | |
[fileManager moveItemAtPath:WebSQLDbFile toPath:newPath error:&err]; | |
} | |
[fileManager createDirectoryAtPath:WebSQLDb withIntermediateDirectories:YES attributes:nil error:&err]; | |
err = nil; | |
[fileManager copyItemAtPath:oldWebKitSQLPath toPath:WebSQLDbFile error:&err]; | |
if (err == nil) | |
[fileManager removeItemAtPath:oldWebKitSQLPath error:&err]; | |
//Set date modified to now (to avoid the phonegap backup fix to replace this file) | |
NSDate *now = [NSDate date]; | |
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:now ,NSFileModificationDate, nil]; | |
[ [NSFileManager defaultManager] setAttributes:attrs ofItemAtPath:WebSQLDbFile error: &err]; | |
needToUpdateDb = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this work on PhoneGap 3?