我在webview中实现了webview和下载管理器。我已成功建立一个操作表来确定文件扩展名,然后拍摄下载或取消按钮。但是,当我点击下载时,它只下载文本,而不是完整文件。如果我将该下载控制方法放在shouldStartLoadWithRequest部分中,它会下载完整文件,但我没有显示任何操作表。下面是我正在使用的2部分代码。任何信息将不胜感激。
//Download manager
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if(navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *theResourcesURL = [request URL];
NSString *fileExtension = [theResourcesURL pathExtension];
NSLog(@"fileExtension is: %@", fileExtension);
if ([fileExtension hasSuffix:@"zip"] || [fileExtension hasSuffix:@"deb"] || [fileExtension hasSuffix:@"rar"] || [fileExtension hasSuffix:@"mp3"] || [fileExtension hasSuffix:@"pdf"] || [fileExtension hasSuffix:@"exe"] || [fileExtension hasSuffix:@"mp4"] || [fileExtension hasSuffix:@"flv"] || [fileExtension hasSuffix:@"torrent"] || [fileExtension hasSuffix:@"png"] || [fileExtension hasSuffix:@"jpg"] || [fileExtension hasSuffix:@"jpeg"]) {
//Action sheet
UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle:[[self->webView.request URL] absoluteString]
delegate:self
cancelButtonTitle:NSLocalizedString(@"Cancel", @"Action sheet cancel button")
destructiveButtonTitle:nil
otherButtonTitles:
NSLocalizedString(@"Download", @"Action sheet button"),
nil]
autorelease];
[actionSheet showFromToolbar:self.navigationController.toolbar];
//Error setting
NSError *error = nil;
// Get documents folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"downloads"];
//Create folder
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
}
return NO;
}
return YES;
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex != actionSheet.cancelButtonIndex) {
NSInteger adjustedIndex = buttonIndex - actionSheet.firstOtherButtonIndex;
switch(adjustedIndex) {
case 0:;
NSURL *theResourcesURL = self->webView.request.URL;
//Download controller
HCDownloadViewController *dlvc = [[HCDownloadViewController alloc] init];
[dlvc downloadURL:theResourcesURL userInfo:nil];
dlvc.delegate = self;
//Configure download view controller
[self.navigationController setNavigationBarHidden:NO];
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0/255.0f green:0/255.0f blue:0/255.0f alpha:1];
[self.navigationController pushViewController:dlvc animated:YES]; break;
}
}
}