#pragma mark-get Request
-(Ibaction) Getrequest: (ID) Sender {
//mode one: using block implementation
//// 1. Create a URL
//Nsurl *url = [Nsurl Urlwithstring:get_url];
//
//// 2. Create Session Object
//Nsurlsession *session = [nsurlsession sharedsession];
//
//// 3. Create Task Request Tasks
//// Nsurlsession is task-based to complete related events
///// Nsurlsessiontask All the tasks are put in this inside to achieve
//Nsurlsessiontask *task = [Session Datataskwithurl:url completionhandler:^ (NSData * _nullable data, Nsurlresponse * _nullable response, Nserror * _nullable error) {
//
//// 4. Parse the relevant data
//if (Error = = nil) {
//Nsdictionary *dict = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments ER Ror:nil];
//
//NSLog (@ "dict =%@", dict);
// }
//
// }];
//
//// 5. Core steps: Start a task [never forget]
/////Reason: The task of Nsurlsessiontask instance is in a suspended state, and if it does not start, the content will not be implemented in block .
//[task resume];
//Method two: Using proxy implementation
//1. Create URL Nsurl *url = [Nsurl Urlwithstring:get_url];
//2. Create Session
//Parameter 1: setting of the Mode
/**
Default session Mode
defaultsessionconfiguration
//Temporary session mode
ephemeralsessionconfiguration
//Background session mode
Backgroundsessionconfigurationwithidentifier
*/
//Parameter 2: Agent
//Parameter 3: Main thread queue
nsurlsession *session = [Nsurlsession sessionwithconfiguration:[nsurlsessionconfiguration Defaultsessionconfiguration] delegate:self delegatequeue:[nsoperationqueue Mainqueue]];
//3. Create Pask
Nsurlsessiondatatask *task = [session Datataskwithurl:url];
//4. Start a task
[Task resume];
//Load data: There is no essential difference between using Nsurlsessiondatatask and Nsurlsessiontask
//To process the download task using this task: Nsurlsessiondownloadtask
//To process upload tasks using: Nsurlsessionuploadtask
}
implementation method of #pragma mark-nsurlsessiondatadelegate protocol
//server starts responding
-(void) Urlsession: (Nsurlsession *) session Datatask: (Nsurlsessiondatatask *) Datatask Didreceiveresponse: ( Nsurlresponse *) Response Completionhandler: (void (^) (nsurlsessionresponsedisposition)) Completionhandler
{
//Allow server response [data is accepted in this place only if the server is allowed to respond]
Completionhandler (nsurlsessionresponseallow);
//Initial HU data, later fragment splicing storage
Self.resultdata = [Nsmutabledata data];
}
//Accept Data Stitching
-(void) Urlsession: (Nsurlsession *) session Datatask: (Nsurlsessiondatatask *) datatask didreceivedata: (NSData *) Data
{
//Repeat, then stitch the first closed clip
[Self.resultdata Appenddata:data];
}
//Data acceptance complete, network request ended
-(void) Urlsession: (Nsurlsession *) session Task: (Nsurlsessiontask *) Task Didcompletewitherror: (NSERROR *) error
{
//parsing
if (Error = = nil) {
nsdictionary *dict = [Nsjsonserialization JSONObjectWithData:self.resultData options:nsjsonreadingallowfragmen TS Error:nil];
NSLog (@ "dict =%@", dict);
}
}
#pragma mark-post Request
-(Ibaction) Postrequest: (ID) Sender {
//1. Creating a URL
Nsurl *url = [Nsurl Urlwithstring:post_url];
//2. Creating a request
nsmutableurlrequest *mutablerequest = [Nsmutableurlrequest requestwithurl:url];
//2.5 Core Settings Boby
nsstring *bobystring = post_body;
NSData *postdata = [bobystring datausingencoding:nsutf8stringencoding];
[mutablerequest sethttpmethod:@ "POST"];
[Mutablerequest Sethttpbody:postdata];
//3. Create Session Object
nsurlsession *session = [nsurlsession sharedsession];
//4. Create Task
Nsurlsessiontask *task = [Session datataskwithrequest:mutablerequest completionhandler:^ (NSData * _Nullable data, Nsurlresponse * _nullable response, Nserror * _nullable error) {
//5. Parsing
if (Error = = nil) {
nsdictionary *dict = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Erro R:nil];
NSLog (@ "dict =%@", dict);
}
}];
//6. Starting a task
[Task resume];
}
Nsurlsession GET request and POST request steps for data parsing