2011-09-11 19 views
1

私はUIWebViewからpngファイルを保存するためにいくつかのコードを使用しています。動作していません。「保存」ボタンを押すと何も起こりません。 「ファイルの保存...」とは別に、ログには何も表示されません。これは、すべてがInterface Builderで正しくリンクされていることをテストすることだけです。ファイルが保存されないのはなぜですか?

-(IBAction)saveFile 
{ 

NSLog(@"Saving File..."); 

NSURL *requestedURL = [webView.request URL]; 

if([[requestedURL pathExtension] isEqualToString:@"png"]) 
{ 
    NSData *fileData = [[NSData alloc] initWithContentsOfURL:requestedURL]; 
    //Store the data locally 
    NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath]  
stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]]; 

    NSString *filePath = [resourceDocPath stringByAppendingPathComponent:[requestedURL lastPathComponent]]; 

    NSError *error = nil; 
    [fileData writeToFile:filePath options:NSDataWritingFileProtectionComplete error:&error]; 

    if(error != nil) { 
     // Failed to write the file 
     NSLog(@"Failed to write file: %@", [error description]); 
    } else { 
     UIAlertView *filenameAlert = [[UIAlertView alloc] initWithTitle:@"File saved" message:[NSString 
stringWithFormat:@"The file %@ has been saved", [requestedURL lastPathComponent]] delegate:self cancelButtonTitle:@"OK" 
otherButtonTitles:nil]; 

     [filenameAlert show]; 
     [filenameAlert release]; 
    } 
} 
+0

あなたは 'resourceDocPath'パスを正しく構築していないかもしれません。私は通常これを使用します: '#define _docs [NSHomeDirectory()stringByAppendingPathComponent:@" Documents "]' –

+0

'AlertView'は表示されますか? – Nekto

答えて

4

あなたは(デバイス上の/ことができ許可されていません)mainBundleのファイルの場所への書き込みをしようと思え:

は、ここで私が使用していたコードです。

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
NSUserDomainMask, YES); 
NSString* resourceDocPath = [paths objectAtIndex:0]; 
関連する問題