Iphone 使用UI web视图下载文件

Iphone 使用UI web视图下载文件,iphone,objective-c,ios,ios4,Iphone,Objective C,Ios,Ios4,可以在我的应用程序中下载实现UI web iphone的文件吗?如果是这样的话,文件将存储在哪里?我如何访问它?你不能仅仅通过浏览来下载文件,但你可以使用 (BOOL)webView:(UIWebView*)webView应加载WithRequest:(NSURLRequest*)请求导航类型:(UIWebViewNavigationType)导航类型 要分析文件的链接(比如通过查看最后一个路径组件的扩展名),如果您希望下载此类文件,可以使用[NSURLConnection connecti

可以在我的应用程序中下载实现UI web iphone的文件吗?如果是这样的话,文件将存储在哪里?我如何访问它?

你不能仅仅通过浏览来下载文件,但你可以使用

  • (BOOL)webView:(UIWebView*)webView应加载WithRequest:(NSURLRequest*)请求导航类型:(UIWebViewNavigationType)导航类型

要分析文件的链接(比如通过查看最后一个路径组件的扩展名),如果您希望下载此类文件,可以使用[NSURLConnection connectionWithRequest:myURLRequest delegate:self];以及所有相关的委托方法,以下载文件并将其存储在documents文件夹中。

如果您想可视化一些已知的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];