file system
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true);
NSString *docDir = [dirPath objectAtIndex:0];
NSLog(@"currentPath %@", docDir);
NSURL *documentsUrl = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] objectAtIndex:0];
NSURL *fileUrl = [documentsUrl URLByAppendingPathComponent:@"testFile.txt"];
NSString *myTestString = @"HELLO WORLD";
[myTestString writeToFile:fileUrl atomically:true encoding:NSUTF8StringEncoding error:nil];
// NSURL *downloadURL = [fileManager URLForDirectory:NSDownloadsDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
//download
// NSFileManager *fileManager = [NSFileManager defaultManager];
//
// NSArray *dirPath = [fileManager URLsForDirectory:NSDownloadsDirectory inDomains:NSUserDomainMask];
//// NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, true);
// NSString *docDir = [dirPath objectAtIndex:0];
//
// NSArray *fileList = [fileManager contentsOfDirectoryAtPath:docDir error:nil];
// for(NSString *fileName in fileList) {
// NSLog(@"array in fileName: %@", fileName);
// }
//customPath 테스트
// NSFileManager *fileManager = [NSFileManager defaultManager];
// NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true);
// NSString *docDir = [dirPath objectAtIndex:0];
//
// NSBundle *bundle = [NSBundle bundleForClass:MLROCRManager2.class];
// NSArray *bundleFiles = [fileManager contentsOfDirectoryAtPath:[bundle bundlePath] error:nil];
//
// for(NSString *fileName in bundleFiles) {
// NSLog(@"array in fileName: %@", fileName);
// NSString *bundlePath = [bundle pathForResource:fileName ofType:nil];
// NSString *savePath = [docDir stringByAppendingPathComponent:fileName]; //저장할 Path
// [fileManager copyItemAtPath:bundlePath toPath:savePath error:nil];
// }
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *dirPath = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
// NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true);
NSURL *docURL = [dirPath objectAtIndex:0];
NSURL *downULR = [docURL URLByAppendingPathComponent:@"Downloads"];
NSData 파일로 저장하기
/*
NSData를 fileName으로 저장하는 함수
@param data NSData
@param fileName 파일 이름(확장자도 필요함)
*/
-(void) saveDataWithData:(NSData*)data withFileName:(NSString*)fileName
{
NSString *documentPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *file;
BOOL flag;
file= [documentPath stringByAppendingPathComponent:fileName];
flag= [data writeToFile:file options:NSDataWritingFileProtectionComplete error:nil];
if (flag) NSLog(@"saving Suceess!!!");
}
www.appcoda.com/files-app-integration
https://life-shelter.tistory.com/147
반응형
'Platform > iOS' 카테고리의 다른 글
[WebView] WKWebView (0) | 2021.08.10 |
---|---|
[iOS] 앱의 생명주기와 앱의 구조 (0) | 2021.05.07 |
[iOS] Camera (0) | 2021.04.28 |
[UI] Points VS Pixel (0) | 2021.04.12 |
[Pods] pod 라이브로리 사용하기 (0) | 2021.03.16 |