Instance Method インスタンスメソッド

downloadTaskWithResumeData:completionHandler:

Creates a download task to resume a previously canceled or failed download and calls a handler upon completion. あるダウンロードタスクを作成して、以前に取り消されたまたは失敗したダウンロードを再開します、そしてあるハンドラを完了に関して呼び出します。

Declaration 宣言

- (NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData *)resumeData 
                                       completionHandler:(void (^)(NSURL *location, NSURLResponse *response, NSError *error))completionHandler;

Parameters パラメータ

resumeData

A data object that provides the data necessary to resume the download. あるデータオブジェクト、それはダウンロードを再開するのに必要なデータを提供します。

completionHandler

The completion handler to call when the load request is complete. This handler is executed on the delegate queue. ロードリクエストが完了する時に呼び出す完了ハンドラ。このハンドラは、委任先キュー上で遂行されます。

If you pass nil, only the session delegate methods are called when the task completes, making this method equivalent to the downloadTaskWithResumeData: method. あなたがnilを渡すならば、セッション委任先メソッドそれらだけがそのタスクが完了する時に呼び出され、このメソッドをdownloadTaskWithResumeData:メソッドと等しいものにします。

location

The location of a temporary file where the server’s response is stored. You must move this file or open it for reading before your completion handler returns. Otherwise, the file is deleted, and the data is lost. サーバのもつ応答が格納されるところの一時ファイルの場所。あなたは、このファイルを移動する、またはそれを読み出すために開くことを、あなたの完了ハンドラが戻る前にしなければなりません。そうでなければ、このファイルは消去されます、そしてデータは失われます。

response

An object that provides response metadata, such as HTTP headers and status code. If you are making an HTTP or HTTPS request, the returned object is actually an NSHTTPURLResponse object. あるオブジェクト、それは応答メタデータを提供します、たとえばHTTPヘッダおよび状態コード。あなたがHTTPまたはHTTPSリクエストをしているならば、返されるオブジェクトは事実上NSHTTPURLResponseオブジェクトです。

error

An error object that indicates why the request failed, or nil if the request was successful. あるエラーオブジェクト、それはなぜリクエストが失敗したかを指し示します、またはnil、もしリクエストがうまくいったならば。

Return Value 戻り値

The new session download task. 新しいセッションダウンロードタスク。

Discussion 議論

By using a completion handler, the task bypasses calls to delegate methods for response and data delivery, and instead provides any resulting data, response, or error inside the completion handler. Delegate methods for handling authentication challenges, however, are still called. 完了ハンドラを使うことによって、タスクは応答とデータを配達するための委任先メソッドへの呼び出しを迂回します、そして代わりに何らかの結果データ、応答、そしてエラーを完了ハンドラ内部で提供します。認証チャレンジを取り扱う委任先メソッドは、しかしながら、依然として呼び出されます。

You should pass a nil completion handler only when creating tasks in sessions whose delegates include a URLSession:downloadTask:didFinishDownloadingToURL: method. あなたがnil完了ハンドラを渡すべきなのは唯一、委任先がURLSession:downloadTask:didFinishDownloadingToURL:メソッドを含むセッションにおいて、それらタスクを作成している時です。

Your app can obtain a resumeData object in two ways: あなたのアプリは、resumeDataオブジェクトを2つの方法で入手します:

  • If your app cancels an existing transfer by calling cancelByProducingResumeData:, the session object passes a resumeData object to the completion handler that you provided in that call. あなたのアプリがある既存の転送をcancelByProducingResumeData:を呼び出すことによって取り消すならば、セッションオブジェクトはresumeDataオブジェクトを、あなたがその呼び出しにおいて提供した完了ハンドラへと渡します。

  • If a transfer fails, the session object provides an NSError object either to its delegate or to the task’s completion handler. In that object, the NSURLSessionDownloadTaskResumeData key in the userInfo dictionary contains a resumeData object. 転送が失敗するならば、セッションオブジェクトは、NSErrorオブジェクトを、それの委任先へまたはタスクのもつ完了ハンドラへのどちらかに提供します。そのオブジェクトにおいて、NSURLSessionDownloadTaskResumeDataキーはuserInfo辞書において、resumeDataオブジェクトを含みます。

After you create the task, you must start it by calling its resume method. あなたがタスクを作成する後、あなたはそれを、それのresumeメソッドを呼び出すことによって開始しなければなりません。

If the request completes successfully, the location parameter of the completion handler block contains the location of the temporary file, and the error parameter is nil. If the request fails, the location parameter is nil and the error parameter contain information about the failure. If a response from the server is received, regardless of whether the request completes successfully or fails, the response parameter contains that information. リクエストが成功裏に完了するならば、完了ハンドラブロックのlocationパラメータは一時ファイルの場所を含みます、そしてerrorパラメータはnilです。リクエストが失敗するならば、locationパラメータはnilです、そしてerrorパラメータはその失敗についての情報を含みます。サーバからの応答が受け取られるならば、リクエストがうまく完了するか失敗するかどうかに関係なく、responseパラメータはその情報を含みます。

See Also 参照

Adding Download Tasks to a Session ダウンロードタスクをセッションに加える