2010-12-14 81 views
1

Je veux récupérer toutes les URL accédées dans le webview et les écrire dans un fichier texte. Je ne veux pas utiliser writeToFile coz in - (BOOL) méthode webView remplacerait au lieu d'ajouter au fichier. Je peux créer un fichier mais tout ce qu'il écrit dans ce fichier est une chaîne que j'ai utilisée pour créer le fichier en utilisant FileManager createFileAtPath avec Content ... Toujours dans la méthode - (BOOL) webView ... quand j'ai essayé de voir si le fichier est lu -only ou writable (isWritableFileAtPath) donne en lecture seule. Permission posix viewDidLoad -> 511 ... Je suis vérifié les attributs de fichier dans le terminal passe à cet endroit son -rwxrwxrwx nouveau Stack Overflow ne sais pas comment afficher le code ici si en utilisant Pastebin ... http://pastebin.com/Tx7CsXVBProblème d'écriture de chaîne dans un fichier ... (SDK iPhone)

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

Répondre

1

Je pense que votre problème peut être que vous testez les documents/file1.txt et non /Documents/file1.txt

Le premier '/' est important

[modifier]
Puis-je faire une suggestion? Distiller cela à ce qui fonctionne d'abord et ensuite comprendre ce qui le fait échouer? Je recommande d'utiliser le formulaire suivant et continuer à partir de là:

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

[/ edit]

+0

S'il utilisait 'stringByAppendingFormat:', alors vous seriez correct. Cependant, il utilise 'stringByAppendingPathComponent:' qui prendra soin du séparateur de chemin. –

+0

Il n'utilise pas ce dernier, il utilise le premier. – KevinDTimm

+0

@KevinDTimm Je suis (semi) corrigé. Il utilise la version 'pathComponent' dans le premier code chunck, et' format' dans le second. Alors oui, vous avez raison. :) –