簡體   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