2013-09-25 2 views
0

안녕하세요 모두 에뮬레이터에서 꽤 잘 작동하는 프로그램을 작성했습니다. 그러나 프로젝트를 자체 장치에 업로드하면 파일을 읽고 항목을 표시 할 수 있지만 파일을 지우고 다시 쓸 때 작업을하지 않습니다.txt 파일 쓰기는 에뮬레이터에서 작동하지만 실제 장치에서는 작동하지 않습니다. xcode

이 문제를 해결할 수있는 방법은 무엇입니까?

+0

코드를 작성할 수 있습니까 – Sauvage

+0

[[NSFileManager defaultManager] removeItemAtPath : filePath 오류 : nil]; [[NSData data] writeToFile : 원자적인 filePath : 예]; NSFileHandle * handle = [NSFileHandle fileHandleForWritingAtPath : filePath]; [handle truncateFileAtOffset : [handle seekToEndOfFile]]; [writeData를 처리하십시오 : [something dataUsingEncoding : NSUTF8StringEncoding]]; – albatross

+0

'NSString * directory = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) [0];'을 디렉토리로 사용하십시오. 그렇다면 전체 경로는 다음과 같습니다.'directory = [directory stringByAppendingPathComponent : fileName];' – Sauvage

답변

2

앱 번들에 파일을 쓰려고하는 것 같습니다. 보안상의 이유로 Apple은 번들을 만지지 못하게합니다. 당신의 파일은 샌드 박스

  • 그렇지 않은 경우에 존재

    • 확인 나는 그것이 작동 생각

    복사 된 파일,

  • 그런 다음 응용 프로그램 번들에서 작업을 복사 : 당신은에 있습니다 시뮬레이터에서 앱을 실행할 때 샌드 박스가 없기 때문에 시뮬레이터에서 앱의 샌드 박스에서 올바른 폴더를 찾는 데 필요한 문서는 다음과 같습니다.

    Sandbox documentation

    File System programming guide

    편집 : 여기

    일부 코드 주어진 디렉토리에 번들에서 파일을 복사 (일부 오타 오류가있을 수, 엑스 코드에서 실행하지 않았다) 문서 디렉토리. 이 코드는 매번 Documents 디렉토리의 파일을 무시하므로주의하십시오. 당신이 복사 한 파일을 작업 할 때
    NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; 
    NSString *fileDirectory = [documentsDirectory stringByAppendingPathComponent:@"MyDirectory"]; 
    
    // Create folder if it does not exist 
    BOOL isDirectory; 
    if (![[NSFileManager defaultManager] fileExistsAtPath:fileDirectory isDirectory:&isDirectory]) 
    { 
        NSError *error; 
        [[NSFileManager defaultManager] createDirectoryAtPath:fileDirectory withIntermediateDirectories:NO attributes:nil error:&error]; 
        if(error) 
        { 
         NSLog(@"Error creating folder : %@",error.localizedDescription); 
         return; 
        } 
        else 
        { 
         NSLog(@"Folder created"); 
        } 
    } 
    
    // Save to disk 
    NSString *filePathOnDisk = [fileDirectory stringByAppendingPathComponent:@"fileInSandbox.foo"]; 
    NSString *pathFromBundle = [[NSBundle mainBundle] pathForResource:@"fileFromBundle" ofType:@"foo"]; 
    NSData *fileData = [NSData dataWithContentsOfFile:pathFromBundle]; 
    NSError *error; 
    if (![fileData writeToFile:filePathOnDisk options:NSDataWritingAtomic error:&error]) 
    { 
        NSLog(@"Error writing file : %@", error.localizedDescription); 
    } 
    else 
    { 
        NSLog(@"File saved to disk successfully"); 
    } 
    

    그런 다음, 우리가 filePathOnDisk에서 만든 경로를 사용합니다.

    파일 관리와 관련된 문서를 다시 읽고 파일 작업에 대해 이해하십시오.

  • +0

    것은 우리가 만든 파일과 display.It 그 파일에서 읽을 수 있지만 문제는 그것이 쓸 수 없거나 그 파일에 추가 할 수있다. – albatross

    +0

    그 이유는 처음으로 앱을 실행할 때 앱의 샌드 박스에 복사해야하는 이유입니다. 그런 다음 복사 된 파일을 편집하기 위해 항상 작업 할 것입니다. – NSZombie

    +0

    어떻게 할 수 있습니까? "앱 번들에서 복사하십시오. 그런 다음 복사 된 파일로 작업하십시오." – albatross

    관련 문제