繁体   English   中英

为什么我的文件没有保存?

[英]Why isn't my file saving?

我正在使用一些代码来保存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]; 
    } 
} 

似乎你试图写入mainBundle的文件位置(设备上不允许/不可能)。 尝试使用查找文档目录

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
NSUserDomainMask, YES); 
NSString* resourceDocPath = [paths objectAtIndex:0]; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM