如何从UIWebView下载文件并再次打开

如何创build一个“下载pipe理器”来检测当你点击一个链接(在UIWebView中)的文件结尾是“.pdf”,“.png”,“.jpeg”,“.tiff”,“.gif” ,“.doc”,“.docx”,“.ppt”,“.pptx”,“.xls”和“.xlsx” ,然后打开UIActionSheet询问您是否要下载或打开。 如果您select下载,则会将该文件下载到设备。

应用程序的另一部分将有一个UITableView中的下载文件的列表,当你点击它们,他们将显示在一个UIWebView,但当然离线,因为他们将本地加载,因为他们已经被下载。

请参阅http://itunes.apple.com/gb/app/downloads-lite-downloader/id349275540?mt=8 ,以更好地了解我所要做的事情。

这样做的最好方法是什么?

使用方法- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType在您的UiWebView的委托,以确定何时要加载资源。

get方法被调用的时候,你只需要从参数(NSURLRequest *)requestparsingURL,如果它是你想要的types之一并且继续你的逻辑(UIActionSheet)则返回NO,如果用户只是简单地点击一个简单的链接到一个HTML文件。

说得通?

Edit_:为了更好地理解一个快速的代码示例

 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { if(navigationType == UIWebViewNavigationTypeLinkClicked) { NSURL *requestedURL = [request URL]; // ...Check if the URL points to a file you're looking for... // Then load the file NSData *fileData = [[NSData alloc] initWithContentsOfURL:requestedURL; // Get the path to the App's Documents directory NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder [fileData writeToFile:[NSString stringWithFormat:@"%@/%@", documentsDirectory, [requestedURL lastPathComponent]] atomically:YES]; } } 

Edit2_:我们已经在讨论聊天中的问题之后更新了代码示例:

 - (IBAction)saveFile:(id)sender { // Get the URL of the loaded ressource NSURL *theRessourcesURL = [[webView request] URL]; NSString *fileExtension = [theRessourcesURL pathExtension]; if ([fileExtension isEqualToString:@"png"] || [fileExtension isEqualToString:@"jpg"]) { // 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 { // File could notbe loaded -> handle errors } } else { // File type not supported } } /** Just a small helper function that returns the path to our Documents directory **/ - (NSString *)documentsDirectoryPath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectoryPath = [paths objectAtIndex:0]; return documentsDirectoryPath; }