Summary:
Nsurlconnection is an older class in iOS network programming, and Nsurlconnection is a good choice when you need a system that is compatible with a lower version.
First, Introduction
After IOS7, Nsurlsession basically replaced the nsurlconnection for network development, after iOS9, nsurlconnection related methods are completely discarded, the iOS system has backward-compatible features, Although Nsurlconnection has been deprecated, its methods can still be used in development, and sometimes it is necessary to use the Nsurlconnection class if it is necessary to be compatible with a very low-version iOS system.
second, using nsurlconnection to synchronize requests
For network requests are divided into synchronous and asynchronous, synchronization refers to the request results before the return, the program code will be stuck at the request, after the code will not be executed, asynchronous refers to send the request, one side in the sub-thread to receive the return data, while executing the code, when the return data received, A callback is used to notify the main thread to do the processing.
Use the following method for Nsurlconnection synchronization requests:
Nsurl * url = [Nsurl urlwithstring:@ "http://www.baidu.com"* request =* data = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil]; NSLog (@ "%@", data); NSLog (@ " continue execution ");
As shown in the print information, you can see that the following code is executed when the data returns to the end:
Iii. using Nsurlconnection for asynchronous requests
The use of synchronous request has a great disadvantage, in the network request, the data return often takes a certain time, it is impossible to complete the instantaneous, the use of synchronous mode will lead to the interface card dead, no prompt and can not interact with any user operation, so that, it is likely to give the user the illusion of the program card dead.
The Nsurlconnection class provides two ways to perform asynchronous request operations.
1. Making asynchronous requests using block
Using the following code to block the asynchronous request, the block will be passed in the request to return data and data information such as parameters:
nsurl * url = [nsurl urlwithstring:@ " http:/ /www.baidu.com * Request = [Nsurlrequest Requestwithurl:url]; // [Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse * _nullable response, NSData * _nullable data,nserror * _nullable Connectionerror) {NSLog ( @ " %@ Span style= "color: #800000;" > " ,data); }]; NSLog ( @ " continue with ");
2. How to use proxy callbacks for asynchronous requests
First abide by the protocol with life a variable nsdata is used to receive data:
@interface Viewcontroller () <NSURLConnectionDataDelegate>{ * _data;} @end
Use the following code to make the request:
_data =* url = [Nsurl urlwithstring:@ "http://www.baidu.com"* request = delegate: self];
Once the request is issued, the following proxy method is invoked to listen for the request process and obtain the data:
-(void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{//Start receiving data[_data setLength:0];}-(void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{//receiving Data[_data appenddata:data];}-(void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error{//failed to receive dataNSLog (@"%@", error);}-(void) Connectiondidfinishloading: (Nsurlconnection *) connection{//Receive data CompletionNSLog (@"%@", _data);}
Original site: http://my.oschina.net/u/2340880/blog/618920
Reference: http://www.cnblogs.com/wendingding/p/3813572.html
Ios:nsurlconnection detailed