====== 파일다운로드 ====== * 레퍼런스 : [[http://developer.apple.com/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLDownload.html#//apple_ref/doc/uid/20001839-BAJEAIEE|NSURLDownload]] NSURLDownload 를 사용해서 사용하려는 URL로부터 컨텐츠를 디스크로 저장하는 기능을 제공. NSURLConnection 과 유사하지만 저장하려는 파일을 명시하는 기능이 추가되었음. 또한 MacBinary, BinHex and gzip 와 같은 인코딩을 디코딩하는 기능이 가능함. NSURLConnection 와 달리 NSURLDownload는 캐쉬에 데이터를 저장하지 않음. Note: 파운데이션 클래스를 쓰는데 문제가 없다면 NSURLDownlolad의 서브클래스인 Web Kit framework 의 WebDownload를 사용해서 인증 인터페이스를 사용할 수 있다. Contents: 친절한 예제코드가 있음 * [[http://developer.apple.com/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLDownload.html#//apple_ref/doc/uid/20001839-165459|Downloading to a Predetermined Destination]] * [[http://developer.apple.com/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLDownload.html#//apple_ref/doc/uid/20001839-163723|Downloading a File Using the Suggested Filename]] * [[http://developer.apple.com/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLDownload.html#//apple_ref/doc/uid/20001839-165094|Displaying the Download Progress]] * [[http://developer.apple.com/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLDownload.html#//apple_ref/doc/uid/20001839-155583|Handling Request Changes]] * [[http://developer.apple.com/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLDownload.html#//apple_ref/doc/uid/20001839-155724|Handling Authentication Challenges]] * [[http://developer.apple.com/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLDownload.html#//apple_ref/doc/uid/20001839-165205|Decoding Encoded Files]] ===== Downloading to a Predetermined Destination ===== One usage pattern for NSURLDownload is downloading a file to a predetermined filename on the disk. If the application knows the destination of the download, it can explicitly set it using setDestination:allowOverwrite:. Multiple setDestination:allowOverwrite: messages to an NSURLDownload instance are ignored. The download starts immediately upon receiving the initWithRequest:delegate: message. It can be canceled any time before the delegate receives a downloadDidFinish: or download:didFailWithError: message by sending the download a cancel message. The example in Listing 1 sets the destination, and thus requires the delegate only implement the download:didFailWithError: and downloadDidFinish: methods. - (void)startDownloadingURL:sender { // create the request NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:CONFIG_SOURCE_URL_STRING] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; // create the connection with the request // and start loading the data NSURLDownload *theDownload = [[NSURLDownload alloc] initWithRequest:theRequest delegate:self]; if (theDownload) { // set the destination file now [theDownload setDestination:CONFIG_SOURCE_PATH allowOverwrite:YES]; } else { // inform the user that the download could not be made } } - (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error { // release the connection [download release]; // inform the user NSLog(@"Download failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSErrorFailingURLStringKey]); } - (void)downloadDidFinish:(NSURLDownload *)download { // release the connection [download release]; // do something with the data NSLog(@"%@",@"downloadDidFinish"); }