0x8badf00d
0x8badf00d

Reputation: 6401

Problem writing string to a file ...(iPhone SDK )

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];
}

Upvotes: 1

Views: 1637

Answers (1)

KevinDTimm
KevinDTimm

Reputation: 14376

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]

Upvotes: 1

Related Questions