1.單例
.h
@interface PackageManager : NSObject
@property (strong, nonatomic) NSString * documentPath;
@property (strong, nonatomic) NSString * cachesPath;
+ (instancetype)shareManager;
.m
static PackageManager * manager;
@implementation PackageManager
+ (instancetype)shareManager {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[self alloc]init];
});
return manager;
}
- (instancetype)init {
if (self = [super init]) {
self.cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
self.documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
}
return self;
}
2. 創(chuàng)建文件夾
.h
/**
創(chuàng)建文件夾
@param filePath 文件夾路徑
*/
- (void)createFolderWithFile:(NSString *)filePath;
.m
- (void)createFolderWithFile:(NSString *)filePath {
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir;
BOOL isExit = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
if (!isExit || !isDir)
{
[fileManager createDirectoryAtPath:filePath
withIntermediateDirectories:YES
attributes:nil
error:nil];
}
}
3.刪除文件夾
.h
/**
刪除文件夾
@param filePath 文件夾路徑
*/
- (void)removeFolderWithFile:(NSString *)filePath;
.m
- (void)removeFolderWithFile:(NSString *)filePath {
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath])
{
[fileManager removeItemAtPath:filePath error:nil];
}
}
3.拷貝文件夾
.h
/**
拷貝文件夾
@param filePath 文件路徑
@param toPath 拷貝路徑
*/
- (void)copyFlolderFrom:(NSString *)filePath to:(NSString *)toPath;
.m
- (void)copyFlolderFrom:(NSString *)filePath to:(NSString *)toPath {
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:toPath])
{
[fileManager removeItemAtPath:toPath error:&error];
}
[fileManager copyItemAtPath:filePath toPath:toPath error:&error];
}
4.拷貝文件
.h
/**
拷貝文件
@param fileName 文件名稱
@param filePath 文件路徑
@param toPath 拷貝路徑
*/
- (void)copyFillWithFile:(NSString *)fileName from:(NSString *)filePath to:(NSString *)toPath;
.m
- (void)copyFillWithFile:(NSString *)fileName from:(NSString *)filePath to:(NSString *)toPath {
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString * fileToPath = [toPath stringByAppendingPathComponent:fileName];
if ([fileManager fileExistsAtPath:fileToPath])
{
[fileManager removeItemAtPath:fileToPath error:&error];
}
[fileManager copyItemAtPath:[filePath stringByAppendingPathComponent:fileName]
toPath:toPath
error:&error];
}
5.移動(dòng)文件夾
.h
/**
移動(dòng)文件夾
@param filePath 文件路徑
@param toPath 移動(dòng)路徑
*/
- (void)moveFolderFrom:(NSString *)filePath to:(NSString *)toPath;
.m
- (void)moveFolderFrom:(NSString *)filePath to:(NSString *)toPath {
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:toPath])
{
[fileManager removeItemAtPath:toPath error:&error];
}
[fileManager moveItemAtPath:filePath toPath:toPath error:&error];
}
6.移動(dòng)文件
.h
/**
移動(dòng)文件
@param fileName 文件名
@param filePath 文件路徑
@param toPath 移動(dòng)路徑
*/
- (void)moveFillWithFill:(NSString *)fileName from:(NSString *)filePath to:(NSString *)toPath;
.m
- (void)moveFillWithFill:(NSString *)fileName from:(NSString *)filePath to:(NSString *)toPath {
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString * fileToPath = [toPath stringByAppendingPathComponent:fileName];
if ([fileManager fileExistsAtPath:fileToPath])
{
[fileManager removeItemAtPath:fileToPath error:&error];
}
[fileManager moveItemAtPath:[filePath stringByAppendingPathComponent:fileName]
toPath:toPath
error:&error];
}