从iphone sdk中的uiwebview下载文件 [英] Downloading files from uiwebview in iphone sdk

查看:127
本文介绍了从iphone sdk中的uiwebview下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从 UIWebView下载文件我在我的 IBAction 事件中使用此代码

Is there any way to download file from UIWebView i am using this code on my IBAction event

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

}
this代码打开 UIWebView 中的文件,我想下载它,当我按下按钮时,打开的文件得到保存。
但我希望我的 UIWebView 表现得像普通浏览器一样,当下载链接出现在其中并且用户按下它时, UIWebView 显示对话框,选项打开或保存,如果用户按下保存文件自动保存,如果用户按下打开,文件应该在 UIWebView 中打开。

} this code open the file in UIWebView , which i want to download and when i press the button the opened file get save. But i want my UIWebView to behave like normal browser , when the download link appear in it and user press it, UIWebView show dialog with option open it or save it if user press save the file get save automatically and if user press open it file should open in UIWebView.

推荐答案

您可以在 UIWebViewDelegate中提供<​​code> webView:shouldStartLoadWithRequest 以便每次用户即将移动到另一个网页时,您都有机会检查链接的样子:

You can provide webView:shouldStartLoadWithRequest in your UIWebViewDelegate so that each time the user is about to move to another web page, you have the chance to check what the link looks like:

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

这篇关于从iphone sdk中的uiwebview下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆