IOS network analysis-(eight NSURLSession Introduction), iosnsurlsession
Just a few minutes ago, Apple officially ordered us to look at the NSURLSession, so we had to seriously follow the boss's instructions and try to recognize the NSURLSession. In fact, the three parties have already solved the problem for us, but we still need to know about NSURLSession. Next, I will give you a brief introduction to NSURLSession.
* The following is a passage written by a Daniel, presented here *
NSURLConnection is rarely used during development. iOS9 has discarded NSURLConnection. Currently, the lowest version is applicable to iOS, so it can be used. NSURLConnection uploads images. You can find your own materials.
Compared with NSURLSession, NSURLConnection has low security. The downloading of NSURLConnection has a peak value, which is troublesome.
Although it is compatible with the lowest version of iOS7, you can also use NSURLSession. AFN does not support NSURLConnection.
NSURLSession: Session. The status is suspended by default. If you want to request a network, you need to enable it.
[NSURLSession sharedSession] obtains the global NSURLSession object. All apps on the iPhone share a global session.
NSURLSessionUploadTask-> NSURLSessionDataTask-> NSURLSessionTask
NSURLSessionDownloadTask-> NSURLSessionTask
Download NSURLSessionDownloadTask, Which is downloaded to the tmp folder by default. After the download is complete, delete the temporary file. So we need to move the file to the Cache before deleting it.
Download Test
/// ViewController. m // CX-NSURLSession overview /// Created by ma c on 16/3/21. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad] ;}// this is a click screen event created for testing. -(Void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {// download test NSURL * url = [NSURL URLWithString: [@ "http: // localhost/tupian.jpg "resume: Resume]; NSURLRequest * request = [NSURLRequest requestWithURL: url]; NSURLSession * session = [NSURLSession sharedSession]; resume * task = [session downloadTaskWithRequest: request completionHandler: ^ (NSURL * _ Nullable location, NSURLResponse * _ Nullable response, NSError * _ Nullable error) {NSLog (@ "download completed"); // response. NSString * cacheParh = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingString: response. suggestedFilename]; NSLog (@ "cache address % @", cacheParh); // obtain the File manager NSFileManager * manager = [NSFileManager defaultManager]; // move the temporary file to the cache directory // [NSURL fileURLWithPath: cachesPath] convert the local path to the URL type // if the address is incorrect, the generated url object is blank [manager moveItemAtURL: location toURL: [NSURL fileURLWithPath: cacheParh] error: NULL] ;}]; // enable the task [task resume] ;}@ end
GET Test 1
/// ViewController. m // CX-NSURLSession overview /// Created by ma c on 16/3/21. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad] ;}// this is a click screen event created for testing. -(Void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {// get test 1 // create url nsurl * url = [NSURL URLWithString: @ "http: // localhost/login. php? Username = haha & password = 123 "]; // create NSURLSession * session = [NSURLSession sharedSession]; // create a task NSURLSessionDataTask * task = [session dataTaskWithURL: url completionHandler: ^ (NSData * _ Nullable data, NSURLResponse * _ Nullable response, NSError * _ Nullable error) {NSLog (@ "% @", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]);}]; // enable the task [task resume];} @ end
GET Test 2
/// ViewController. m // CX-NSURLSession overview /// Created by ma c on 16/3/21. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad] ;}// this is a click screen event created for testing. -(Void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {// get Test 2 // create url nsurl * url = [NSURL URLWithString: @ "http: // localhost/login. php? Username = haha & password = 123 "]; // create a request NSURLRequest * request = [NSURLRequest requestWithURL: url]; // create NSURLSession * session = [NSURLSession sharedSession]; // create a task NSURLSessionDataTask * task = [session dataTaskWithRequest: request completionHandler: ^ (NSData * _ Nullable data, NSURLResponse * _ Nullable response, NSError * _ Nullable error) {NSLog (@ "% @", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]) ;}]; // enable the task [task resume] ;}@ end
POST Test
/// ViewController. m // CX-NSURLSession overview /// Created by ma c on 16/3/21. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad] ;}// this is a click screen event created for testing. -(Void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {// post test // create url nsurl * url = [NSURL URLWithString: @ "http: // localhost/login. php "]; // create a request for NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url]; request. HTTPMethod = @ "POST"; request. HTTPBody = [@ "username = haha & password = 123" dataUsingEncoding: NSUTF8StringEncoding]; // create NSURLSession * session = [NSURLSession sharedSession]; // create a task NSURLSessionDataTask * task = [session dataTaskWithRequest: request completionHandler: ^ (NSData * _ Nullable data, NSURLResponse * _ Nullable response, NSError * _ Nullable error) {NSLog (@ "% @", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]) ;}]; // enable the task [task resume] ;}@ end