2012-08-17 60 views
3

我想將文檔從文檔複製到iOS中的資源文件夾中。如何將文檔中的文件複製到iOS應用程序中?

沒有資源文檔。

從文檔到資源的文件。

所以我寫了下面的代碼。

- (NSString *) getDBPath2 
{ 
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Htoo.mp3"]; 

    return dbPath; 
} 

- (void) copyDatabaseIfNeeded 
{ 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSString *dbPath = [self getDBPath]; 
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 

    if(success) 
    { 
     NSString *defaultDBPath = [[NSBundle mainBundle] resourcePath]; 
     success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error]; 

     if (!success) 
      NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    } 
} 

- (NSString *) getDBPath 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 
    NSString *documentsDir = [paths objectAtIndex:0]; 
    return [documentsDir stringByAppendingPathComponent:@"Htoo.mp3"]; 
} 

當我寫上面的代碼時,我收到了錯誤信息。

2012-08-17 23:55:03.482 TestProjects[1645:f803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to create writable database file with message 'The operation couldn’t be completed. (Cocoa error 516.)'.' 

那麼如何將文件從文檔複製到iOS中的資源?

感謝您的閱讀。

+0

你的意思是應用程序綁定? – J2theC 2012-08-17 17:35:37

+0

yes.application包。 – MrDeveloper 2012-08-17 17:36:19

+0

我該怎麼做? – MrDeveloper 2012-08-17 17:38:42

回答

4

您的應用程序(Bundle)的資源是隻讀的,發佈後您無法修改您的包。捆綁軟件包是在編譯時由Xcode創建的,並且不能在運行時對其進行修改。一旦安裝了應用程序,有很多方法可以存儲數據:NSDefaults,sqlite,Core Data,文檔目錄。

0

如果你想保存的MP3,那麼你最好將其保存在緩存:

NSString *desPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
相關問題