Platform/iOS

[iOS] FileSystem

개랭갱깽스타 2021. 4. 22. 15:46

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!!!");
}

 

zeddios.tistory.com/440

 

iOS ) FileManager를 이용해 파일/폴더 만드는 법

안녕하세요 :) Zedd입니다. 제목이 뭔가 추상적인데.....이 글은...저를 위한....공부... FileManager를 이용해 파일/폴더 만드는 법 파일 및 디렉토리와 관련된 가장 기본적인 작업 중 일부는 파일 시스

zeddios.tistory.com

www.appcoda.com/files-app-integration

 

How to Integrate Your App with Files App in iOS 11 | Swift Tutorial

In this tutorial, I'll show you how to embrace iOS 11's Files app. First, I'll walk you through configuration of an app so that any files stored in its iOS

www.appcoda.com

https://life-shelter.tistory.com/147

 

[iOS] NSData 파일로 저장하기

디버그를 위해 raw데이터를 그냥 파일에 저장할 일이 자꾸 생겨서 함수형식으로 하나 만듬 App내의 Documents 폴더에 저장된다. 이 파일은 iExplorer나 iFunBox등으로 볼 수 있다. /*  NSData를 fileName으로 

life-shelter.tistory.com

 

반응형

'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