#import "ViewController.h"@interfaceViewcontroller () <NSURLSessionDataDelegate>/** Receive response body information*/@property (nonatomic, strong) Nsmutabledata*FileData;@end@implementationViewcontroller-(Nsmutabledata *) filedata{if(_filedata = =Nil) {_filedata=[Nsmutabledata data]; } return_filedata;}-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{ //1.urlNsurl *url = [Nsurl urlwithstring:@"Http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]; //2. Create a Request objectNsurlrequest *request =[Nsurlrequest Requestwithurl:url]; //3. Create the Session object, set the proxy /*First parameter: configuration information [nsurlsessionconfiguration defaultsessionconfiguration] Second parameter: Agent third parameter: Sets the Agent method in which thread calls */nsurlsession*session = [Nsurlsession sessionwithconfiguration:[nsurlsessionconfiguration defaultsessionconfiguration]Delegate: Self delegatequeue:[nsoperationqueue mainqueue]]; //4. Create a taskNsurlsessiondatatask *datatask =[Session datataskwithrequest:request]; //5. Execute Task[Datatask resume];}#pragmaMark----------------------#pragmaMark Nsurlsessiondatadelegate/** 1. Receive a response to the server it cancels the request by default * * @param Session Object * @param datatask Request Task * @param response Response header information * @param Completionhandler callback to the system*/-(void) Urlsession: (Nsurlsession *) session Datatask: (Nsurlsessiondatatask *) datatask didreceiveresponse: (NSURLResponse *) Response Completionhandler: (void(^) (nsurlsessionresponsedisposition)) completionhandler{NSLog (@"%s", __func__); /*nsurlsessionresponsecancel = 0, cancel default Nsurlsessionresponseallow = 1, receive Nsurlsessionresponsebecomedownload = 2, becomes the download task Nsurlsessionresponsebecomestream into stream*/Completionhandler (Nsurlsessionresponseallow);}/** * receive data calls returned by the server multiple times * * @param Session Object * @param datatask Request task * @param data Data for this download*/-(void) Urlsession: (Nsurlsession *) session Datatask: (Nsurlsessiondatatask *) datatask didreceivedata: (NSData *) data{NSLog (@"%s", __func__); //Stitching Data[Self.filedata appenddata:data];}/** * when the request ends or fails, call * * @param Session Object * @param datatask Request task * @param error Information*/-(void) Urlsession: (Nsurlsession *) session Task: (Nsurlsessiontask *) Task Didcompletewitherror: (Nserror *) error{NSLog (@"%s", __func__); //parsing DataNSLog (@"%@", [[NSString Alloc]initwithdata:self.filedata encoding:nsutf8stringencoding]);}@end
(1) Create Nsurlsession object, set proxy (default configuration)
"' OBJC
1. Create the nsurlsession and set up the proxy
/*
The first parameter: Global configuration settings for the session object, typically using the default configuration to
Second argument: Who becomes the delegate of the Session object
The third parameter: in which queue the proxy method executes (called in which thread), if the home column is executed in the main thread, and if it is a non-home column, execute in the child thread
*/
Nsurlsession *session = [Nsurlsession sessionwithconfiguration:[nsurlsessionconfiguration Defaultsessionconfiguration] delegate:self delegatequeue:[nsoperationqueue Mainqueue]];
```
(2) Create a Nsurlsessiondatatask task (post and get selection) based on session object
"' OBJC
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.)
"' OBJC
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)
"' OBJC
/*
1. Call when a server response is received
Session: The Session object that sent the request
Datatask: Task task created according to Nsurlsession
Response: Server response Information (response header)
Completionhandler: 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: (nonnull void (^) ( nsurlsessionresponsedisposition)) Completionhandler
/*
2. Called when the data returned by the server is received
The method may be called multiple times
*/
-(void) Urlsession: (nonnull nsurlsession *) session Datatask: (nonnull nsurlsessiondatatask *) datatask Didreceivedata: ( Nonnull NSData *) data
/*
3. Call this method when the request is complete
Either the request succeeds or the request fails, and 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)
"' OBJC
By default, when a server response is received, the server considers that the client does not need to receive data, so the following proxy methods do not invoke the
If you need to continue receiving the data returned by the server, you need to call block and pass in the corresponding policy
/*
Nsurlsessionresponsecancel = 0, canceling the task
Nsurlsessionresponseallow = 1, receiving task
Nsurlsessionresponsebecomedownload = 2, converted to download
Nsurlsessionresponsebecomestream ns_enum_available (10_11, 9_0) = 3, transformed into a stream
*/
Completionhandler (Nsurlsessionresponseallow);
```
---
iOS Development Network Eight: Nsurlsession related Agent method