gpt4 book ai didi

iphone - 从iphone sdk中的uiwebview下载文件

转载 作者:可可西里 更新时间:2023-11-01 04:36:41 24 4
gpt4 key购买 nike

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

最佳答案

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

关于iphone - 从iphone sdk中的uiwebview下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8076347/

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