ios down move file method summary
- 2020-05-09 19:21:49
- OfStack
This objective c code is used to move files in the specified path
if ([fileManager copyItemAtPath:@"FilePath1"
toPath:@"FilePath2" error:NULL]) {
NSLog(@"Copied successfully");
}
Method 2:
Using NSFileManager:
Let your document path and your cache path. Walk through all the files and move them around using NSFileManager
- (void) moveAllDocs {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
NSString *sourceDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *destinationDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:sourceDirectory error:&error];
for(NSString *sourceFileName in contents) {
NSString *sourceFile = [sourceDirectory stringByAppendingPathComponent:sourceFileName];
NSString *destFile = [destinationDirectory stringByAppendingPathComponent:sourceFileName];
if(![fileManager moveItemAtPath:sourceFile toPath:destFile error:&error]) {
NSLog(@"Error: %@", error);
}
}
}
Method 3:
FCFileManager is an iOS file management tool built on top of NSFileManager to simplify file management. It provides many static methods for performing the most common operations in a few lines of code. It works as a default file directory, allowing relative paths, but it works easily in any other directory.
Move file:
[FCFileManager moveItemAtPath:@"test.txt" toPath:@"tests/test.txt"];
Remove file:
//remove file at the specified path
[FCFileManager removeItemAtPath:@"test.txt"];
That's the end of this article, I hope you enjoy it.