gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 10:14:06 26 4
gpt4 key购买 nike

我如何创建一个“下载管理器”来检测您点击的链接(在 UIWebView 中)何时具有以 ".pdf"、".png"、".jpeg"、".tiff 结尾的文件", ".gif", ".doc", ".docx", ".ppt", ".pptx", ".xls"and ".xlsx" 然后会打开一个 UIActionSheet 询问你是否想下载或打开。如果您选择下载,它会将该文件下载到设备。

应用程序的另一部分将在 UITableView 中包含已下载文件的列表,当您点击它们时,它们将显示在 UIWebView 中,但当然是离线的,因为它们会像下载时一样在本地加载。

参见 http://itunes.apple.com/gb/app/downloads-lite-downloader/id349275540?mt=8以便更好地理解我正在尝试做的事情。

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

最佳答案

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

当方法 get 被调用时,您只需要从参数 (NSURLRequest *)request 中解析 URL,如果它是您想要的类型之一则返回 NO 并继续您的逻辑 (UIActionSheet)如果用户只是点击了一个指向 HTML 文件的简单链接,则返回 YES。

有道理吗?

编辑_:为了更好地理解快速代码示例

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

关于ios - 如何从 UIWebView 下载文件并再次打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7377565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com