Last active
August 29, 2015 14:00
-
-
Save amitittyerah/11399061 to your computer and use it in GitHub Desktop.
Copy db files to app documents
This file contains hidden or 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
// DatabaseFileManager.h | |
#import <Foundation/Foundation.h> | |
@interface DatabaseFileManager : NSObject | |
+ (void) manageFiles :(NSArray *) arrayOfDatabaseNames; | |
@end | |
// DatabaseFileManager.m | |
#import "DatabaseFileManager.h" | |
@implementation DatabaseFileManager | |
+ (void) manageFiles:(NSArray *)arrayOfDatabaseNames | |
{ | |
for(NSString *databaseName in arrayOfDatabaseNames) | |
{ | |
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *databasePath = [[documentPaths objectAtIndex:0] stringByAppendingPathComponent:databaseName]; | |
[self createAndCheckDatabase:databasePath nameOfDatabase:databaseName]; | |
} | |
} | |
+ (void) createAndCheckDatabase : (NSString *)databasePath nameOfDatabase: (NSString *)databaseName | |
{ | |
BOOL success; | |
NSError *error; | |
NSFileManager *fileManager = [NSFileManager defaultManager]; | |
success = [fileManager fileExistsAtPath:databasePath]; | |
if(success) return; | |
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; | |
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:&error]; | |
NSLog(@"Error : %@", error); | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment