2011-03-16 106 views

回答

144

我不知道什么是wget的,但要获得从Web文件并将其存储在本地,你可以使用NSData的:

NSString *stringURL = @"http://www.somewhere.com/thefile.png"; 
NSURL *url = [NSURL URLWithString:stringURL]; 
NSData *urlData = [NSData dataWithContentsOfURL:url]; 
if (urlData) 
{ 
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.png"]; 
    [urlData writeToFile:filePath atomically:YES]; 
} 
+3

只是想知道,这是阻塞或不?我会假设这个块。 – schystz 2013-02-08 15:34:28

+6

@schystz如果通过阻止你的意思是同步的,那么是的。 – carlossless 2013-02-19 13:45:53

+0

同样,也有人可能对'+ [NSString stringWithContentsOfURL:encoding:error:]' – Zmaster 2013-10-20 13:17:08

6

我想更简单的方法是使用ASIHTTPRequest。三行代码可以做到这一点:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
[request setDownloadDestinationPath:@"/path/to/my_file.txt"]; 
[request startSynchronous]; 

Link to Reference

更新:我应该指出,ASIHTTPRequest不再维持。作者特别建议人们改用其他框架,比如AFNetworking

1

前段时间我实现了一个易于使用的“下载管理器”库:PTDownloadManager。你可以试试!

+0

非常好的你。谢谢。 – 2015-03-09 14:39:58

0

有这么多的方法:

  1. NSURL

  2. ASIHTTP

  3. libcurl

  4. easyget,商业一个具有强大的功能。

+0

是ASIHTTP还是支持的地方?该网站建议使用其他的东西:http://allseeing-i.com/ASIHTTPRequest/ – Robert 2013-06-05 13:48:54

+1

不可以。不要使用ASIHTTP。对于高级别的工具,请使用AFNetworking:https://github.com/AFNetworking/AFNetworking – GnarlyDog 2014-05-21 23:00:05

13

我将使用结束块使用异步访问。

本示例将Google徽标保存到设备的文档目录中。 (iOS 5以上,OSX 10.7+)

NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; 
NSString *filePath = [documentDir stringByAppendingPathComponent:@"GoogleLogo.png"]; 

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.google.com/images/srpr/logo11w.png"]]; 
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
    if (error) { 
     NSLog(@"Download Error:%@",error.description); 
    } 
    if (data) { 
     [data writeToFile:filePath atomically:YES]; 
     NSLog(@"File is saved to %@",filePath); 
    } 
}]; 
11

NSURLSession在IOS 7引入,是下载文件的推荐的SDK方式。无需导入第三方库。

NSURL *url = [NSURL URLWithString:@"http://www.something.com/file"]; 
NSURLRequest *downloadRequest = [NSURLRequest requestWithURL:url]; 
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; 
NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil]; 
self.downloadTask = [self.urlSession downloadTaskWithRequest:downloadRequest]; 
[self.downloadTask resume]; 

然后,您可以使用NSURLSessionDownloadDelegate委托方法来监视错误,请下载完成,下载进度等等......有内嵌块完成处理程序回调方法太多,如果你喜欢。苹果文档解释您何时需要使用另一个。

有这些文章的读:

objc.io NSURLConnection to NSURLSession

URL Loading System Programming Guide

+1

这是否异步工作 – 2016-04-06 05:24:17

+0

Network API不能异步工作会很奇怪。所以是的,NSURLSession API是高度异步的:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/UsingNSURLSession.html – bandejapaisa 2016-05-12 16:07:42

+0

处理委托部分很烦人。 – 2017-11-01 02:14:53

相关问题