7.1 Related to knowledge points
(1) How to implement file upload
/* First parameter: Request object second parameter: request body (file data to upload) block callback: NSData: Response body nsurlresponse: Response header Nserror: Requested error message * /*uploadtask = [Session uploadtaskwithrequest:request Fromdata:data completionhandler:^ (NSData * __nullable data, Nsurlresponse * __nullable response, Nserror * __nullable Erro R
(2) Set up Agent to listen for file upload progress in Agent method
/* call this method to upload the file data if the file data is large, then the method is called multiple parameter descriptions: totalbytessent: The size of the file data that has been uploaded */-(void) Urlsession: (nonnull nsurlsession *) session task: (nonnull nsurlsessiontask *) Task Didsendbodydata: (int64_t) BytesSent totalbytessent: (int64_t) totalbytessent totalbytesexpectedtosend: (int64_t) totalBytesExpectedToSend{ NSLog (@ "%.2f",1.0 * totalbytessent/ totalbytesexpectedtosend);}
(3) About nsurlsessionconfiguration related
01 functions: Nsurlsession can be configured uniformly, such as request timeout, etc.
02 Ways to create and use
//three ways to create a configuration+ (Nsurlsessionconfiguration *) defaultsessionconfiguration;+ (Nsurlsessionconfiguration *) ephemeralsessionconfiguration;+ (Nsurlsessionconfiguration *) Backgroundsessionconfigurationwithidentifier: (NSString *) identifier ns_available (10_10, 8_0);//Unified Configuration Nsurlsession-(Nsurlsession *) session{if(_session = =Nil) { //Create NsurlsessionconfigurationNsurlsessionconfiguration *config =[Nsurlsessionconfiguration defaultsessionconfiguration]; //Set request Timeout to 10 secondsConfig.timeoutintervalforrequest =Ten; //whether to continue the request (upload or download) in the case of a cellular networkConfig.allowscellularaccess =NO; _session= [Nsurlsession sessionwithconfiguration:configDelegate: Self delegatequeue:[nsoperationqueue mainqueue]]; } return_session;}
Nsurlsession implementing file Uploads