Nsurlsession Download file-agent

Source: Internet
Author: User
Tags configuration settings

-3.1 involving knowledge points

(1) Create Nsurlsession object, set proxy (default configuration)
// 1. Create the nsurlsession and set up the proxy    /*      The first parameter: The global configuration settings for the session object, typically using the default configuration, can be the     second parameter: Who becomes the delegate of the session object the     third argument: in which queue the proxy method executes (called in which thread), If it is the home column then executes in the main thread, and if it is a non-host column     , execute *    /delegatein the child thread: Self Delegatequeue:[nsoperationqueue Mainqueue]];

(2) Create a Nsurlsessiondatatask task (post and get selection) based on session object
// Create Task Nsurl *url = [Nsurl urlwithstring:@ "http://120.25.226.186:32812/resources/images/minion_01.png " ]; // Note: If you want to send a POST request, then use Datataskwithrequest to set some request header information Nsurlsessiondatatask *datatask = [session Datataskwithurl:url];

(3) Perform tasks (other methods, such as suspension, cancellation, etc.)
    // Start Task     [Datatask resume];     // other methods, such as canceling a task, pausing a task, etc.     [datatask cancel];    [Datatask suspend];

(4) Obey the agent agreement and implement the Proxy method (3 related agent methods)
1The session is called when the server response is received: The session object that sent the request Datatask: Task task created based on nsurlsession response: Server response Information (response header) complet Ionhandler: Tells the server whether to receive the returned data through the block callback-(void) Urlsession: (nonnull nsurlsession *) session Datatask: (nonnull nsurlsessiondatatask *) datatask Didreceiveresponse: ( Nonnull nsurlresponse *) Response Completionhandler: (nonnullvoid(^) (nsurlsessionresponsedisposition)) Completionhandler2the method that is called when the data returned by the server is received may be called multiple times-(void) Urlsession: (nonnull nsurlsession *) session Datatask: (nonnull nsurlsessiondatatask *) datatask Didreceivedata: ( Nonnull NSData *) Data3The method is called when the request is complete, whether the request succeeds or the request fails, if the request fails, then the Error object has a value, otherwise the Error object is empty-(void) Urlsession: (nonnull nsurlsession *) session task: (nonnull nsurlsessiontask *) Task Didcompletewitherror: (Nullable Nserror *) Error

(5) When the server response is received, tell the server to receive data (call block)
    By default, when a server response is received, the server considers that the client does not need to receive data, so the subsequent proxy method does not invoke the need    to continue receiving the data returned by the server if it is    necessary to call block and pass in the corresponding policy /*         nsurlsessionresponsecancel = 0, cancel task        Nsurlsessionresponseallow = 1,  receive task        Nsurlsessionresponsebecomedownload = 2, converted to download        nsurlsessionresponsebecomestream ns_enum_available (10_11, 9_0) = 3 , transformed into streams     *    /Completionhandler (nsurlsessionresponseallow);

Nsurlsession Download file-agent

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.