将字符串写入文件时出现问题...(iPhone SDK)

时间:2010-12-14 19:14:55

标签: iphone file-io sdk

我想获取在webview中访问的所有URL并将它们写入文本文件。我不想在 - (BOOL)webView方法中使用writeToFile,它会覆盖而不是附加到文件。我可以创建文件,但它写入该文件的所有内容都是我用来创建文件的字符串,使用FileManager createFileAtPath with Content ... 同样在 - (BOOL)webView方法中...当我试图查看文件是只读还是可写(isWritableFileAtPath)时,它给出了只读。 viewDidLoad中的POSIX权限 - > 511 ...... 检查终端中的文件属性到达该位置的-rwxrwxrwx 我是Stack Overflow的新手,不知道如何在这里发布代码,所以使用pastebin ... http://pastebin.com/Tx7CsXVB

- (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 个答案:

答案 0 :(得分:1)

我认为您的问题可能是您正在测试Documents / file1.txt而不是/Documents/file1.txt

领先的'/'很重要

[编辑]
我可以提一个建议吗?将其提炼到最先发挥作用,然后找出导致失败的原因? 我建议使用以下表格并从那里继续:

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

[/编辑]