0

I want create one application that read file from document dir but first I want check document dir if not exist file in self copy this file (Db.sqlite) from NSBundle Main to document dir and read it.

I could read data from document dir or check document dir but I don't know how to copy file from NSBundle to document dir.

please guide me about it.

david
  • 147
  • 3
  • 12

1 Answers1

4

Try

    - (void)copyDatabaseIfNeeded
    {    
        BOOL success;

        NSFileManager *fileManager = [NSFileManager defaultManager];
        success = [fileManager fileExistsAtPath:[self getDBPath]];
        if(success)
        {
            return;// If exists, then do nothing.
        }
        //Get DB from bundle & copy it to the doc dirctory.
        NSString *databasePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"DB.sqlite"];
        [fileManager copyItemAtPath:databasePath toPath:[self getDBPath] error:nil];
    }

    //Check DB in doc directory.
    - (NSString *)getDBPath
    {
           NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
           NSString *documentsDir = [paths objectAtIndex:0];
           return [documentsDir stringByAppendingPathComponent:@"DB.sqlite"];
    }

It will copy your DB in doc directory, then you can used it from doc directory.

Girish
  • 4,692
  • 4
  • 35
  • 55