2010-12-14 13 views
1

webviewでアクセスされたすべてのURLを取得してテキストファイルに書きたいと思います。writeToFile cozを使用したくありません - (BOOL)webView method itファイルに追加するのではなく上書きします。私はファイルを作成することができますが、そのファイルに書き込む文字列はすべてFileManagerのcreateFileAtPathを使ってファイルを作成するために使用した文字列です... - (BOOL)WebViewメソッド...ファイルを読み込もうとしたとき-onlyまたはwritable(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

は私が

先頭に「/」が重要である

[編集]
私は月あなたの問題は、あなたがドキュメント/ FILE1.TXTをテストしていないが/Documents/file1.txtことかもしれないと思います提案をする?これを最初にうまくいくまで蒸留してから、何が失敗するのか把握します。彼は `stringByAppendingFormatを使用していた場合

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

[/編集]

+0

: 私は次の形式を使用して、そこから継続推薦'、あなたは正しいだろう。しかし、彼は 'stringByAppendingPathComponent:'を使用しています。これはパスの区切り文字を処理します。 –

+0

彼は後者を使用していない、彼は前者を使用しています。 – KevinDTimm

+0

@KevinDTimm私は(半)立っている。彼は最初のコードchunckで 'pathComponent'バージョンを使い、2番目のコードで' format'を使っています。はい、あなたは正しいです。 :) –

関連する問題