2010-12-14 4 views
1

webview에서 액세스 한 모든 URL을 가져 와서 텍스트 파일에 쓰고 싶습니다. writeToFile coz를 사용하고 싶지 않습니다. - (BOOL) webView method it 파일에 추가하는 대신 덮어 씁니다. 파일을 만들 수 있지만 그 파일에 쓰는 것은 모두 FileManager의 createFileAtPath를 사용하여 파일을 만드는 데 사용 된 문자열입니다. - (BOOL) webView 메서드에서도 ... 파일을 읽었는지 확인하려고했을 때 쓰기 전용 (isWritableFileAtPath)은 읽기 전용입니다. 있는 viewDidLoad에서 POSIX 권한 -> 511 ... 파일이 해당 위치로가는 터미널 속성 검사는 내가 페이스트 빈 사용 그래서 여기에 코드를 게시하는 방법을 모르는 스택 오버플로에 새로운 오전 -rwxrwxrwx ... http://pastebin.com/Tx7CsXVB문자열을 파일에 쓰는 데 문제가 있습니다 ... (iPhone SDK)

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [myBrowser loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com"]]]; // UIWebView *myBrowser; is an instance variable 
    NSFileManager *fileMgr=[NSFileManager defaultManager]; 
    NSDictionary* fileAttrs = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:777] forKey:NSFilePosixPermissions]; /*for setting attribute to rwx for all users */ 
    NSDictionary *attribs; // To read attributes after writing something to file 
    NSString *aPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/file1.txt"]; 
    myBrowser.delegate = self; // UIWebView delegate Self 
    // if the File Doesn't exist create one 
    if([fileMgr fileExistsAtPath:aPath]) 
    { 
     NSLog(@"File Exists at this Location"); 
    } 
    else 
    { 
     NSString *someString = @"This is start of file"; 
     NSData *startString =[someString dataUsingEncoding: NSASCIIStringEncoding]; 
     [fileMgr createFileAtPath:aPath contents:startString attributes: fileAttrs]; // earlier attributes was nil changed to fileAttrs 
    } 
    NSLog(@"aPath is %@",aPath); 
    attribs = [fileMgr attributesOfItemAtPath:aPath error: NULL]; 
    NSLog (@"Created on %@", [attribs objectForKey: NSFileCreationDate]); 
    NSLog (@"File type %@", [attribs objectForKey: NSFileType]); 
    NSLog (@"POSIX Permissions %@", [attribs objectForKey: NSFilePosixPermissions]); 
} 

//UIWebView delegate calls this method every time user touches any embedded URL's in the current WebPage. I want to grab all the URL's accessed and write them to file. 
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { 
     NSFileManager *fileManager =[NSFileManager defaultmanager]; 
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/file1.txt"]; 
    fileHandle = [NSFileHandle fileHandleForWritingAtPath:path]; 
    [fileHandle seekToEndOfFile]; // Moved up next to fileHandleForWritingAtPath since the above would place pointer to start of file again so setting handle to seek to End of File 
    /* section of code to check if the file at that path is writable or not */ 
    if ([fileManager isWritableFileAtPath:path] == YES) 
     NSLog (@"File is writable"); 
    else 
     NSLog (@"File is read only"); 
    /* section of code to check if the file at that path is writable or not ENDS*/ 
    NSURL *url = request.URL; 
    NSString *currenturl = url.absoluteString; 
    NSString *currentURL = [NSString stringWithFormat:@"%@\n",currenturl]; 
    NSString *str =[NSString stringWithFormat:@"%@",currentURL];/* has already been set up */ 
    [fileHandle writeData:[str dataUsingEncoding:NSUTF8StringEncoding]]; 
    // testing if string has been written to file by reading it... 
    NSData *dataBuffer = [fileMgr contentsAtPath:path]; 
    NSString *some; 
    some = [[NSString alloc] initWithData:dataBuffer encoding:NSASCIIStringEncoding]; 
    NSLog(@"SOme String is: %@",some); 
    [fileHandle closeFile]; 
} 

답변

1

나는

선행 '/'중요

[편집]
내가 일 문제는이 문서 /은 File2.txt을 테스트하고하지 않는이 /Documents/file1.txt 있다는 것 같아요 제안 해? 이것을 먼저 무엇이 효과가 있는지 파악한 다음 실패하게 만드는 원인을 찾아 내십시오. 나는 다음과 같은 양식을 사용하고 거기에서 계속 추천 :

if ([fileManager isWritableFileAtPath: @"/Documents/file1.txt"] == YES) 
    NSLog (@"File is writable"); 
else 
    NSLog (@"File is read only"); 

[/ 편집]

+0

그는'stringByAppendingFormat를 사용한 경우 :', 당신은 올바른 것. 그러나 그는'stringByAppendingPathComponent :'를 사용하여 경로 구분자를 처리합니다. –

+0

그는 후자를 사용하지 않고 전자를 사용하고 있습니다. – KevinDTimm

+0

@KevinDTimm 나는 (반쯤) 고쳤다. 그는 첫 번째 코드 chunck에서'pathComponent' 버전을 사용하고 두 번째 코드에서는'format'을 사용하고 있습니다. 네, 맞습니다. :) –

관련 문제