2013-05-16 3 views
0

문서 dir에서 파일을 읽는 하나의 응용 프로그램을 만들고 싶습니다. 그러나 먼저 NSBundle Main의이 파일 (Db.sqlite)을 자체 복사본에 존재하는 파일이 있는지 확인하고 싶습니다. dir을 읽고 그것을 읽으십시오.Document dir에서 파일을 검사하고 NSBundle에서 복사하는 방법

문서 dir에서 데이터를 읽거나 문서 dir을 검사 할 수는 있지만 파일을 NSBundle에서 문서 dir로 복사하는 방법을 모르겠습니다.

안내해주세요.

답변

4

그런 다음 문서 디렉토리에서 사용할 수 있습니다, 그것은 doc 디렉토리에 DB를 복사합니다

- (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"]; 
    } 

을보십시오.