如何从UIWebView下载文件并再次打开 [英] How to download files from UIWebView and open again
问题描述
如何创建一个下载管理器,它会检测您点击的链接(在UIWebView中)是否以.pdf,.png,.jpeg,.tiff, .gif,.doc,.docx,.ppt,.pptx,.xls和.xlsx,然后打开一个UIActionSheet,询问您是否要下载或打开。如果您选择下载,则会将该文件下载到设备。
How can I create a "download manager" which would detect when a link you tap (in a UIWebView) has the file ending ".pdf", ".png", ".jpeg", ".tiff", ".gif", ".doc", ".docx", ".ppt", ".pptx", ".xls" and ".xlsx"and then would open a UIActionSheet asking you if you would like to download or open. If you select download, it will then download that file to the device.
应用程序的另一部分将在UITableView中列出下载的文件,当您点击它们时,它们将显示在UIWebView中,但当然还有离线,因为它们将在本地加载,因为它们将被下载。
Another section of the app would have a list of downloaded files in a UITableView and when you tap on them, they will show in a UIWebView, but of course offline because they would load locally as they would have been downloaded.
请参阅 http://itunes.apple.com/gb/app/downloads-lite-downloader/id349275540?mt=8 ,以更好地了解我是什么
See http://itunes.apple.com/gb/app/downloads-lite-downloader/id349275540?mt=8 for a better understanding of what I am trying to do.
这是最好的方法是什么?
What is the best way of doing this?
谢谢,
James
推荐答案
使用方法 - (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)请求在您的UiWebView代理中确定何时加载资源的naviType请求navigationType(UIWebViewNavigationType)navigationType
。
当调用该方法时,您只需要从参数(NSURLRequest *)请求
中解析URL,并返回NO,如果它是您想要的类型之一,并继续使用您的逻辑(UIActionSheet),或返回YES,如果用户只需点击一个简单的链接到一个HTML文件。
When the method get's called, you just need to parse the URL from the parameter (NSURLRequest *)request
, and return NO if it's one of your desired type and continue with your logic (UIActionSheet) or return YES if the user just clicked a simple link to a HTML file.
感觉?
Edit_:更好地了解快速代码示例
Edit_: For better understanding a quick code example
- (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;
}
这篇关于如何从UIWebView下载文件并再次打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!