给出URL时下载文件 - iPhone sdk

时间:2011-06-24 11:00:34

标签: iphone ios4 download nsurlconnection nsurl

当给出URL时,是否可以从文档目录中下载文件存储在文档目录中?我在apple开发者网站上浏览了一些文档。它表示可能使用NSURLDownload,但它不能在iOS中使用。因此,应该只在iOS的情况下使用NSURLConnection来完成。所以,有人帮我从给定的URL下载文件(甚至是HTML页面)。提前致谢。

3 个答案:

答案 0 :(得分:4)

以下是从服务器获取图像并将其保存到文件的示例。

创建实例变量

NSMutableData *activeDownload;
NSURLConnection *imageConnection;

现在创建你的连接

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSURL *url = [[NSURL alloc] initWithString:@"URL TO FILE"];
[request setURL:url];
[url release];
url = nil;

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

处理接收数据

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.activeDownload appendData:data];
}

传输完毕,保存数据

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:self.imageFileName];

[self.activeDownload writeToFile:fileName atomically:YES];

self.activeDownload = nil;
self.imageConnection = nil;
}

我还没有显示其他代码(属性等),但我认为这应该足以帮助你了。

答案 1 :(得分:1)

我认为ASIHTTPRequest库可以帮助您:)

  

<强> ASIWebPageRequest

     

ASIHTTPRequest附带的ASIWebPageRequest类允许您下载完整的网页,包括图像和样式表等外部资源。

来自allseeing-i.com

的代码示例
- (IBAction)loadURL:(NSURL *)url
{
   // Assume request is a property of our controller
   // First, we'll cancel any in-progress page load
   [[self request] setDelegate:nil];
   [[self request] cancel];

   [self setRequest:[ASIWebPageRequest requestWithURL:url]];
   [[self request] setDelegate:self];
   [[self request] setDidFailSelector:@selector(webPageFetchFailed:)];
   [[self request] setDidFinishSelector:@selector(webPageFetchSucceeded:)];

   // Tell the request to embed external resources directly in the page
   [[self request] setUrlReplacementMode:ASIReplaceExternalResourcesWithData];

   // It is strongly recommended you use a download cache with ASIWebPageRequest
   // When using a cache, external resources are automatically stored in the cache
   // and can be pulled from the cache on subsequent page loads
   [[self request] setDownloadCache:[ASIDownloadCache sharedCache]];

   // Ask the download cache for a place to store the cached data
   // This is the most efficient way for an ASIWebPageRequest to store a web page
   [[self request] setDownloadDestinationPath:
      [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:[self request]]];

   [[self request] startAsynchronous];
}

- (void)webPageFetchFailed:(ASIHTTPRequest *)theRequest
{
   // Obviously you should handle the error properly...
   NSLog(@"%@",[theRequest error]);
}

- (void)webPageFetchSucceeded:(ASIHTTPRequest *)theRequest
{
   NSString *response = [NSString stringWithContentsOfFile:
      [theRequest downloadDestinationPath] encoding:[theRequest responseEncoding] error:nil];
   // Note we're setting the baseURL to the url of the page we downloaded. This is important!
   [webView loadHTMLString:response baseURL:[request url]];
}

答案 2 :(得分:0)

是您可以使用NSURLConnection执行此操作。您需要向URL发出请求。像这样的东西。

NSString *strDownloadURL = @"File URL";
NSMutableURLRequest *request  = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:strDownloadURL]];

Connection *con = [[[Connection alloc] init];
con.delegate=self;
[[[NSURLConnection alloc]initWithRequest:request delegate:con] autorelease];

现在,在Connection Delegate方法中,didReceiveData将NSData分配给全局变量。

- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data {
  if (receivedData != nil) {
    [receivedData appendData:data];
  } else {
    receivedData = [[NSMutableData alloc] initWithData:data];
  }
}

最后,在连接委托方法“connectionDidFinishLoading”你应该有NSData格式的文件数据。所以将它存储在Document目录中就像这样。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  [receivedData writeToFile:DocumentPathtoSaveFile atomically:YES];
}