2011-03-13 53 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]; 

}