我希望在应用程序启动时将多个文件从我的NSBundle复制到Documents目录。
这是复制文件的代码:
- (NSString *) getPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingString:@"Photo.png"];
}
- (void) copyFile
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *Path = [self getPath];
BOOL success = [fileManager fileExistsAtPath:Path];
if(!success) {
NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Photo.png"];
success = [fileManager copyItemAtPath:defaultPath toPath:Path error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable file with message '%@'.", [error localizedDescription]);
}
}
将在'applicationDidFinishLaunchingWithOptions'方法中调用copyFile方法。但是此方法仅允许将一个文件复制到Documents目录。如果我想从我的NSBundle复制50个文件,这是否意味着我必须指定50个路径?有没有更短的方法,例如只用几行代码获取NSBundle中的所有文件?
答案 0 :(得分:6)
您可以复制资源文件夹中的所有文件,但我怀疑您是否真的想这样做,是吗?毕竟这些也是应用程序资源(如字符串文件或仅用于UI按钮的图形等)
因此,您可能希望在资源文件夹中有一个子目录(例如,名为FilesToCopy
),其中包含您要复制的所有文件(例如Photo.png
处的Contents/Resources/FilesToCopy/Photo.png
)
要获取目录的内容,只需使用
NSError * err;
NSArray * files;
NSString * srcPath;
NSString * dstPath;
NSFileManager * man;
srcPath = [self getSrcPath];
dstPath = [self getDstPath];
man = [[NSFileManager alloc] init];
files = [man contentsOfDirectoryAtPath:srcPath error:&err];
然后您可以一次复制一个文件:
for (NSString *aFile in files) {
BOOL success;
NSString * srcFile = [srcPath stringByAppendingPathComponent:aFile];
NSString * dstFile = [dstPath stringByAppendingPathComponent:aFile];
success = [man copyItemAtPath:srcFile toPath:dstFile error:&err];
// Verify success
}
现在您只需要srcPath
和dstPath
。
- (NSString *)getSrcPath
{
return [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"FilesToCopy"];
}
- (NSString *)getDstPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}
答案 1 :(得分:0)
更改你的功能原型:
- (NSString *) getPathofFileName:(NSString*)name;
- (void) copyFileWithName:(NSString*)name;
答案 2 :(得分:0)
是的,你确定你能做到这一点
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
NSMutableArray *filesArray = [NSMutableArray arrayWithObjects:@"0.epub",@"1.epub",@"2.epub",@"3.epub",@"4.epub",nil];//here put the name of files you want to copy
[self copyFilesToDocuments:filesArray];
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;
self.window.rootViewController = (UIViewController*)self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void) copyFilesToDocuments : (NSMutableArray*)filesArr {
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
for(int i = 0 ; i < [filesArr count] ; i++) {
NSString *filePath = [docDir stringByAppendingPathComponent:[filesArr objectAtIndex:i]];
NSFileManager *fm = [NSFileManager defaultManager];
BOOL exist = [fm fileExistsAtPath:filePath];
NSError *error = nil;
if (!exist) {
NSString *path = [[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:[filesArr objectAtIndex:i]];
BOOL success = [fm copyItemAtPath:path toPath:filePath error:&error];
if (!success) {
NSAssert1(0,@"%@",[error localizedDescription]);
}
}
}
}