2011-03-16 107 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

相關問題