iPhone앱안에 있는 /document 저장공간내 파일을 제어할때 필요한 메소드를이다. 내용이야 간단하지만 만들려고 하면 여간 귀찮은게 아니라서 만들어 놓고 쓸 생각이다. 급조한거라 좀 부실하지만 다듬어가면서 업데이트해야지.
이번 프로젝트의 경우 특이하게 파일관련 기능을 자주 썻지만(드롭박스 같은 클라우드 API를 포함해서) 다음 프로젝트에선 어떨지 모르겠다.
1. 파일복사.
- (BOOL)copyFile:(NSString *)srcPath destination:(NSString *)destPath error:(NSError **)error {
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDirectory;
BOOL result = NO;
if([self isExistFile:srcPath isDirectory:&isDirectory] == YES) {
if (isDirectory) {
NSString *preFolderPath = [destPath stringByDeletingLastPathComponent];
[fileManager createDirectoryAtPath: preFolderPath
withIntermediateDirectories: YES
attributes: nil
error: error];
result = [fileManager copyItemAtPath:srcPath toPath:destPath error:error];
return result;
} else {
result = [fileManager copyItemAtPath:srcPath toPath:destPath error:error];
return result;
}
}else {
*error = [NSError errorWithErrorCode:ErrorCodeFileNotFoundError];
return NO;
}
}
2. 파일삭제
- (BOOL)removeFile:(NSString *)filePath error:(NSError **)error {
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDirectory;
BOOL result = NO;
if([self isExistFile:filePath isDirectory:&isDirectory] == YES) {
if (isDirectory) {
NSArray *contents = [fileManager contentsOfDirectoryAtPath:filePath error:error];
for(int i=0;i<[contents count];i++) {
NSString *childFileName = [contents objectAtIndex:i];
NSString *nextFilePath =[[filePath stringByAppendingString:@"/"] stringByAppendingString:childFileName];
result = [self removeFile:nextFilePath error:error];
}
result = [fileManager removeItemAtPath:filePath error:error];
return result;
} else {
result = [fileManager removeItemAtPath: filePath error:error];
return result;
}
} else {
*error = [NSError errorWithErrorCode:ErrorCodeFileNotFoundError];
return NO;
}
}
3. 이름변경하기
- (BOOL)renameFile:(NSString *)filePath newfilename:(NSString *)newFilePath error:(NSError **)error {
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDirectory;
BOOL result = NO;
if([self isExistFile:filePath isDirectory:&isDirectory] == YES) {
if (isDirectory) {
NSString *preFolderPath = [newFilePath stringByDeletingLastPathComponent] ;
[fileManager createDirectoryAtPath: preFolderPath
withIntermediateDirectories: YES
attributes: nil
error: error];
result = [fileManager moveItemAtPath:filePath toPath:newFilePath error:error];
return result;
}else {
NSString *preFolderPath = [newFilePath stringByDeletingLastPathComponent] ;
[fileManager createDirectoryAtPath: preFolderPath
withIntermediateDirectories: YES
attributes: nil
error: error];
result = [fileManager moveItemAtPath:filePath toPath:newFilePath error:error];
return result;
}
} else {
*error = [NSError errorWithErrorCode:ErrorCodeFileNotFoundError];
return NO;
}
}
4. 파일이동하기.
- (BOOL)moveFile:(NSString *)srcPath destination:(NSString *)destPath error:(NSError **)error {
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDirectory;
BOOL result = NO;
if([self isExistFile:srcPath isDirectory:&isDirectory] == YES) {
if (isDirectory) {
NSString *preFolderPath = [destPath stringByDeletingLastPathComponent] ;
[fileManager createDirectoryAtPath: preFolderPath
withIntermediateDirectories: YES
attributes: nil
error: error];
result = [fileManager moveItemAtPath:srcPath toPath:destPath error:error];
return result;
}else {
NSString *preFolderPath = [destPath stringByDeletingLastPathComponent] ;
[fileManager createDirectoryAtPath: preFolderPath
withIntermediateDirectories: YES
attributes: nil
error: error];
result = [fileManager moveItemAtPath:srcPath toPath:destPath error:error];
return result;
}
} else {
*error = [NSError errorWithErrorCode:ErrorCodeFileNotFoundError];
return NO;
}
}
5. 파일정보보기
- (NSDictionary *)showPropertyFile:(NSString *)filePath {
NSError *error = nil;
NSDictionary *attr = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error];
if (error == nil) {
return attr;
}
return nil;
}
6. 새폴더 만들기.
- (BOOL)createFolder:(NSString *)currentDirectory folderName:(NSString *)foldername error:(NSError **)error {
NSFileManager *filemanager = [NSFileManager defaultManager];
NSString *newFolderPath = [currentDirectory stringByAppendingFormat:@"/%@",foldername];
BOOL isDirectory;
if([self isExistFile:newFolderPath isDirectory:&isDirectory] == YES) {
if (isDirectory) {
return NO;
}else {
BOOL result = [filemanager createDirectoryAtPath: newFolderPath
withIntermediateDirectories: YES
attributes: nil
error:error];
return result;
}
}else {
BOOL result = [filemanager createDirectoryAtPath: newFolderPath
withIntermediateDirectories: YES
attributes: nil
error:error];
return result;
}
}
'코딩(プログラミング)' 카테고리의 다른 글
XCode에서 pngcrush caught libpng error 의 컴파일 에러발생시 대처방법 (0) | 2013.10.11 |
---|---|
XCode에서 Doxyzen용 코멘트 자동으로 생성하는 팁 (0) | 2013.09.28 |
iPhone(iOS) 도큐먼트 폴더내 파일사용하기 (0) | 2013.09.26 |
XCode5 새로운기능 Images.xcassets 사용하기 (0) | 2013.09.13 |
Git에서 .gitignore 사용법 (소스관리 대상파일을 설정하는 방법) (0) | 2013.08.16 |
iPhone스타일? AdHoc 배포용 다운로드 페이지를 만들어보자. (0) | 2013.08.15 |
댓글을 달아 주세요