【问题标题】:Downloading files from uiwebview in iphone sdk从 iphone sdk 中的 uiwebview 下载文件
【发布时间】:2011-11-10 07:52:07
【问题描述】:

有什么方法可以从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 中打开。

【问题讨论】:

    标签: iphone ios uiwebview


    【解决方案1】:

    您可以在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
      }
    

    【讨论】:

    • 感谢塞尔吉奥的回答,感谢您的回复,您的评论行指导了我很多:)
    • @Sergio 如果我尝试下载一个文件(可能来自雅虎/交换服务器),它的方案将是 bold https bold 和pathExtension 是一些随机字符串,例如。 ashx 等...!如何提示下载此类文件!对此有什么想法吗?
    猜你喜欢
    • 2011-04-29
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多