在iPhone SDK中从UIWebView下载文件

5

有没有办法从UIWebView下载文件。我在我的IBAction事件上使用了这段代码。

- (IBAction)saveFile:(id)sender {
// Get the URL of the loaded ressource
NSURL *theRessourcesURL = [[self.webDisplay request] URL];
NSString *fileExtension = [theRessourcesURL pathExtension];

if ([fileExtension isEqualToString:@"png"] || [fileExtension isEqualToString:@"jpg"] || 
    [fileExtension isEqualToString:@"pdf"] || [fileExtension isEqualToString:@"html"]) {
    // Get the filename of the loaded ressource form the UIWebView's request URL
    NSString *filename = [theRessourcesURL lastPathComponent];
    NSLog(@"Filename: %@", filename);
    // Get the path to the App's Documents directory
    NSString *docPath = [self documentsDirectoryPath];
    // Combine the filename and the path to the documents dir into the full path
    NSString *pathToDownloadTo = [NSString stringWithFormat:@"%@/%@", docPath, filename];


    // Load the file from the remote server
    NSData *tmp = [NSData dataWithContentsOfURL:theRessourcesURL];
    // Save the loaded data if loaded successfully
    if (tmp != nil) {
        NSError *error = nil;
        // Write the contents of our tmp object into a file
        [tmp writeToFile:pathToDownloadTo options:NSDataWritingAtomic error:&error];
        if (error != nil) {
            NSLog(@"Failed to save the file: %@", [error description]);
        } else {
            // Display an UIAlertView that shows the users we saved the file :)
            UIAlertView *filenameAlert = [[UIAlertView alloc] initWithTitle:@"File saved" message:[NSString stringWithFormat:@"The file %@ has been saved.", filename] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [filenameAlert show];
            [filenameAlert release];
        }
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" 
                                                        message:@"File could not be loaded" 
                                                       delegate:nil 
                                              cancelButtonTitle:@"Okay" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        // File could notbe loaded -> handle errors
    }
} else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" 
                                                    message:@"File type not supported" 
                                                   delegate:nil 
                                          cancelButtonTitle:@"Okay" 
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
    // File type not supported
}

这段代码在UIWebView中打开文件,我想要下载并在按下按钮时保存已打开的文件。 但是当下载链接出现在UIWebView中,并且用户按下它时,我希望UIWebView像普通浏览器一样运行,显示对话框并提供打开或保存选项。 如果用户选择保存,文件将自动保存,如果用户选择打开,则文件应在UIWebView中打开。

1个回答

6
您可以在您的UIWebViewDelegate中提供webView:shouldStartLoadWithRequest,以便每次用户即将移动到另一个网页时,您有机会检查链接的样子。
 - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {

     if ([[[request URL] scheme] isEqual:@"http"] && 
         [[[request URL] pathExtension]...])
            <your download/save code here>
            return NO;  //-- no need to follow the link
     }
     return YES; //-- otherwise, follow the link
  }

1
谢谢Sergio的回答和响应,你的注释指导了我很多 :) - Muhammad Saqib
@Sergio 如果我尝试下载一个文件(可能来自雅虎/交换服务器),它将具有方案为 粗体 https 粗体,路径扩展名是一些随机字符串,例如 ashx 等...!我如何提示下载这样的文件!对此有什么想法吗? - Nirav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接