当前位置:  开发笔记 > 前端 > 正文

从iphone sdk中的uiwebview下载文件

如何解决《从iphonesdk中的uiwebview下载文件》经验,为你挑选了1个好方法。

有没有办法从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> sergio..:

您可以提供webView:shouldStartLoadWithRequest,UIWebViewDelegate以便每次用户即将移动到另一个网页时,您都有机会检查链接的外观:

 - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {

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

推荐阅读
wangtao
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有