2012-08-17 40 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]; 
相关问题