In a short time and not long ago, Apple formally ordered us to look to nsurlsession, so we have to seriously listen to the boss's teachings, and strive to recognize nsurlsession. In fact, the three sides have already solved the problem for us, but we still need to know about nsurlsession. Below, I will briefly introduce nsurlsession for you.
* Here's a quote from a Daniel who wrote
Nsurlconnection in the development will use less, iOS9 has been nsurlconnection obsolete, now the minimum version is generally suitable for iOS, so it can also be used. Nsurlconnection upload Pictures, you can find information on their own.
Nsurlconnection is less secure than nsurlsession. Nsurlconnection Download Peak, more trouble to deal with.
Although the minimum version of IOS7 is appropriate, you can also use Nsurlsession. AFN has not supported nsurlconnection.
Nsurlsession: Session. The default is the pending state, which needs to be turned on if you want to request a network.
[Nsurlsession Sharedsession] Gets the global Nsurlsession object. Share a global session with all apps on the iphone.
Nsurlsessionuploadtask, Nsurlsessiondatatask, Nsurlsessiontask
Nsurlsessiondownloadtask-Nsurlsessiontask
Nsurlsessiondownloadtask download, the default download to the TMP folder. Delete the temporary files after the download is complete. So we're going to move it to the cache before deleting the file.
Download test
////VIEWCONTROLLER.M//cx-nsurlsession Introduction////Created by Ma C on 16/3/21.//copyright©2016 year Xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; }//This is a click-through screen event created for testing purposes. -(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{ //Download TestNsurl* URL = [Nsurl urlwithstring:[@"http://localhost/tupian.jpg"Stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]]; Nsurlrequest* Request =[Nsurlrequest Requestwithurl:url]; Nsurlsession* Session =[Nsurlsession sharedsession]; Nsurlsessiondownloadtask* task = [session downloadtaskwithrequest:request completionhandler:^ (Nsurl * _nullable location, Nsurlresponse * _Nullab Le response, Nserror *_nullable Error) {NSLog (@"Download Complete"); //the resource file name in the Response.suggestedfilename response informationNSString * Cacheparh =[[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) Lastobject] StringByAppendingString:response.suggestedFilename]; NSLog (@"Cache Address%@", Cacheparh); //Get File ManagerNsfilemanager * Manager =[Nsfilemanager Defaultmanager]; //move temporary files to the cache directory//[Nsurl fileurlwithpath:cachespath] convert local path to URL type//URL If the address is incorrect, the resulting URL object is empty[manager moveitematurl:location Tourl:[nsurl Fileurlwithpath:cacheparh] error:null]; }]; //Open Task[Task resume];}@end
GET Test One
////VIEWCONTROLLER.M//cx-nsurlsession Introduction////Created by Ma C on 16/3/21.//copyright©2016 year Xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; }//This is a click-through screen event created for testing purposes. -(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{ //Get test One//Create URLNsurl * url = [Nsurl urlwithstring:@"http://localhost/login.php?username=haha&password=123"]; //Create NsurlsessionNsurlsession * session =[Nsurlsession sharedsession]; //Create a taskNsurlsessiondatatask * task = [session Datataskwithurl:url completionhandler:^ (NSData * _nullable data, Nsurlresponse * _N Ullable Response, Nserror *_nullable Error) {NSLog (@"%@", [[NSString Alloc]initwithdata:data encoding:nsutf8stringencoding]); }]; //Open Task[Task resume]; }@end
GET Test Two
////VIEWCONTROLLER.M//cx-nsurlsession Introduction////Created by Ma C on 16/3/21.//copyright©2016 year Xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; }//This is a click-through screen event created for testing purposes. -(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{ //get test Two//Create URLNsurl * url = [Nsurl urlwithstring:@"http://localhost/login.php?username=haha&password=123"]; //Create requestNsurlrequest * Request =[Nsurlrequest Requestwithurl:url]; //Create NsurlsessionNsurlsession * session =[Nsurlsession sharedsession]; //Create a taskNsurlsessiondatatask* task = [session datataskwithrequest:request completionhandler:^ (NSData * _nullable data, Nsurlresponse * _nullable RESPO NSE, Nserror *_nullable Error) {NSLog (@"%@", [[NSString Alloc]initwithdata:data encoding:nsutf8stringencoding]); }]; //Open Task[Task resume]; }@end
POST Test
////VIEWCONTROLLER.M//cx-nsurlsession Introduction////Created by Ma C on 16/3/21.//copyright©2016 year Xubaoaichiyu. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; }//This is a click-through screen event created for testing purposes. -(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{ //Post Test//Create URLNsurl * url = [Nsurl urlwithstring:@"http://localhost/login.php"]; //Create requestNsmutableurlrequest * Request =[Nsmutableurlrequest Requestwithurl:url]; Request. HttpMethod=@"POST"; Request. Httpbody= [@"username=haha&password=123"datausingencoding:nsutf8stringencoding]; //Create NsurlsessionNsurlsession * session =[Nsurlsession sharedsession]; //Create a taskNsurlsessiondatatask* task = [session datataskwithrequest:request completionhandler:^ (NSData * _nullable data, Nsurlresponse * _nullable RESPO NSE, Nserror *_nullable Error) {NSLog (@"%@", [[NSString Alloc]initwithdata:data encoding:nsutf8stringencoding]); }]; //Open Task[Task resume]; }@end
An analysis of the IOS network-(eight nsurlsession introduction)