app bundle与sandbox 目录文件拷贝

这儿有段代码,教大家如何拷贝文件。

 

- (void) copySampleFilesToMyDocumentsFolder { NSError *error; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *bundleRoot = [[NSBundle mainBundle] bundlePath]; NSArray *dirContents = [fileManager contentsOfDirectoryAtPath: bundleRoot error: &error]; NSArray *onlyPdf = [dirContents filteredArrayUsingPredicate: [NSPredicate predicateWithFormat: @"self ENDSWITH '.jpg'"]]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [paths objectAtIndex: 0]; for (int i = 0; i < onlyPdf.count; i++) { NSString *pdfName = [onlyPdf objectAtIndex: i]; NSString *docPdfFilePath = [documentsDir stringByAppendingPathComponent: pdfName]; //Using NSFileManager we can perform many file system operations. BOOL success = [fileManager fileExistsAtPath: docPdfFilePath]; if (!success) { NSString *samplePdfFile = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: pdfName]; success = [fileManager copyItemAtPath: samplePdfFile toPath: docPdfFilePath error: &error]; if (!success){} // NSAssert1(0, @"Failed to copy file '%@'.", [error localizedDescription]); //debugLog(@"Failed to copy %@ file, error %@", pdfName, [error localizedDescription]); else { //debugLog(@"File copied %@ OK", pdfName); } } else { //debugLog(@"File exits %@, skip copy", pdfName); } } }  

你可能感兴趣的:(File,System,sandbox)