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

uploadTaskWithRequest:fromFile:completionHandler:

Creates a task that performs an HTTP request for uploading the specified file, then calls a handler upon completion. あるタスクを作成します、それはあるHTTPリクエストをこの指定されたファイルをアップロードするために実行します、それからあるハンドラを完了に際して呼び出します。

Declaration 宣言

- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request 
                                         fromFile:(NSURL *)fileURL 
                                completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler;

Parameters パラメータ

request

An NSURLRequest instance that provides the URL, cache policy, request type, and so on. The body stream and body data in this request object are ignored. あるNSURLRequestインスタンス、それはURL、キャッシュ方針、リクエスト型、などを提供します。このリクエストオブジェクトの中のボディストリームとボディデータは、無視されます。

fileURL

The URL of the file to upload. アップロードするファイルのURL。

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 uploadTaskWithRequest:fromFile: method. あなたがnilを渡すならば、セッション委任先メソッドそれらだけがそのタスクが完了する時に呼び出され、このメソッドをuploadTaskWithRequest:fromFile:メソッドと等しいものにします。

This completion handler takes the following parameters: この完了ハンドラは、以下のパラメータを取ります:

data

The data returned by the server. サーバによって返されるデータ。

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 upload task. 新しいセッションアップロードタスク。

Discussion 議論

An HTTP upload request is any request that contains a request body, such as a POST or PUT request. Upload tasks require you to create a request object so that you can provide metadata for the upload, like HTTP request headers. HTTPアップロードリクエストは、あるリクエストボディ、たとえばPOSTPUTリクエストなど、を含んでいる何らかのリクエストです。アップロードタスクは、あなたにリクエストオブジェクトを作成するよう要求します、そうすることであなたはメタデータをそのアップロードに提供できます、HTTPリクエストヘッダのように。

Unlike uploadTaskWithRequest:fromFile:, this method returns the response body after it has been received in full, and does not require you to write a custom delegate to obtain the response body. uploadTaskWithRequest:fromFile:とは違い、このメソッドは応答ボディをそれが完全に受け取られる後に返します、そしてあなたにあつらえの委任先を記述して応答ボディを入手するよう要求しません。

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. 完了ハンドラを使うことによって、タスクは応答とデータを配達するための委任先メソッドへの呼び出しを迂回します、そして代わりに何らかの結果データ、応答、そしてエラーを完了ハンドラ内部で提供します。認証チャレンジを取り扱う委任先メソッドは、しかしながら、依然として呼び出されます。

Typically you usually pass a nil completion handler only when creating tasks in sessions whose delegates include a URLSession:dataTask:didReceiveData: method. However, if you do not need the response data, use key-value observing to watch for changes to the task’s status to determine when it completes. 概してあなたが通常nil完了ハンドラを渡すのは、委任先がURLSession:dataTask:didReceiveData:メソッドを含むセッションにおいて、それらタスクを作成している時のみです。しかしながら、あなたが応答データを必要としないならば、キー値監視を使って、タスクのもつ状態の変化を監視して、それが完了する時を決定してください。

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

If the request completes successfully, the data parameter of the completion handler block contains the resource data, and the error parameter is nil. If the request fails, the data parameter is nil, and the error parameter contains 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. リクエストが成功裏に完了するならば、完了ハンドラブロックのdataパラメータはリソースデータを含みます、そしてerrorパラメータはnilです。リクエストが失敗するならば、dataパラメータはnilです、そしてerrorパラメータはその失敗についての情報を含みます。サーバからの応答が受け取られるならば、リクエストがうまく完了するか失敗するかどうかに関係なく、responseパラメータはその情報を含みます。

See Also 参照

Adding Upload Tasks to a Session アップロードタスクをセッションに加える