Skip to content

Instantly share code, notes, and snippets.

@amitittyerah
Last active August 29, 2015 14:00
Show Gist options
  • Save amitittyerah/11399061 to your computer and use it in GitHub Desktop.
Save amitittyerah/11399061 to your computer and use it in GitHub Desktop.
Copy db files to app documents
// 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