Problem writing string to a file …(iPhone SDK )

删除回忆录丶 提交于 2019-12-11 06:39:02

问题


I want to grab all URL's accessed in webview and write them to a text file.I don't want to use writeToFile coz in -(BOOL)webView method it would overwrite instead of appending to file. I can create file but all it writes to that file is string I used to create the file using FileManager createFileAtPath with Content ... Also in the -(BOOL) webView method... when I tried to see if the file is read-only or writable ( isWritableFileAtPath ) it gives read-only. POSIX Permissions in viewDidLoad -> 511 ... checked file attributes in terminal going to that location its -rwxrwxrwx I am new to Stack Overflow don't know how to post code here so using 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:


I think your issue may be that you're testing Documents/file1.txt and not /Documents/file1.txt

The leading '/' is important

[edit]
May I make a suggestion? Distill this down to what works first and then figure out what makes it fail? I would recommend using the following form and continuing from there:

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

[/edit]



来源:https://stackoverflow.com/questions/4443184/problem-writing-string-to-a-file-iphone-sdk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!