2011-03-13 21 views

回答

1

你真的无法下载文件只需通过浏览到他们,但你可以做的是使用

  • (BOOL)webView的:(UIWebView的*)webView的shouldStartLoadWithRequest:(*的NSURLRequest)请求navigationType:(UIWebViewNavigationType )navigationType

分析(链接为一个文件,说通过看最后一个部分的扩展名),如果你想这种文件被下载超过您可以用[NSURLConnection的connectionWithRequest:myURLRequest委托:自我]。及其所有关联的委托方法下载文件并将其存储在文档文件夹中。

0

如果你想显示一些已知的webview文件,它会自动显示你...但是如果页面回传一个未知的webview文件(这发生在我与Citrix ica文件)webview会给你一个错误。 ......要解决这个问题,我用这个代码(注意,在这里,我将允许只下载ICA文件,但你可以改变这种状况):

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 
{ 

if([error code]!=102) 
{  [self.lblError setText:[NSString stringWithFormat:@"%@",error]]; 
     return; 
    } 

NSDictionary *userInfo = [error userInfo]; 
NSString * url = [[NSString alloc] initWithFormat:@"%@",[userInfo objectForKey:@"NSErrorFailingURLKey"] ]; 


NSString *search = @"?"; 
NSRange result = [url rangeOfString:search]; 
NSInteger startingPosition; 
NSString *fileName,*fileExtention,*fileLocation; 
if (result.location != NSNotFound) { 
    startingPosition = result.location + result.length; 
    fileLocation = [url substringToIndex:result.location]; 
    fileExtention=[fileLocation pathExtension]; 
} 
else 
{ 
    fileLocation=url; 
} 

fileName = [fileLocation lastPathComponent]; 
fileExtention=[fileLocation pathExtension]; 

//check if file to download if ica file 
if(![fileExtention isEqualToString:@"ica"]) 
    return; 

self.lblError.textColor=[UIColor blackColor]; 
self.lblError.text=[NSString stringWithFormat:@"downloading %@...",fileName]; 

NSURL * _url = [[NSURL alloc] initWithString:url]; 


// Get file online 
NSData *fileOnline = [[NSData alloc] initWithContentsOfURL:_url]; 

// Write file to the Documents directory 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
if (!documentsDirectory) { 
    // NSLog(@"Documents directory not found!"); 
    return; 
} 
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName]; 

//NSLog(@"appFile path: %@",appFile); 
[fileOnline writeToFile:appFile atomically:YES]; 

NSURL* aUrl = [NSURL fileURLWithPath:appFile]; 

self.interactionController = [UIDocumentInteractionController interactionControllerWithURL: aUrl]; 
self.interactionController.delegate = self; 
self.lblError.text=[NSString stringWithFormat:@"%@ downloaded",fileName]; 
[self.interactionController presentOpenInMenuFromRect:self.lblError.frame inView:self.view animated:YES]; 

}