Nsurlconnection can be very convenient for sending synchronous or asynchronous network requests
+ (NSData *) Sendsynchronousrequest: (nsurlrequest *) Request Returningresponse: (Nsurlresponse *) Response error: ( Nserror * *) error+ (void) Sendasynchronousrequest: (nsurlrequest *) request queue: (Nsoperationqueue *) queue Completionhandler: (void (^) (Nsurlresponse *response, NSData *data, Nserror *connectionerror)) handler
For various reasons, Apple began to abandon nsurlconnection in iOS9.0, instead of using nsurlsession instead.
The NSData data class, the method of getting data from a URL to create an object, is a synchronous request, and the drawback is that the answer object cannot be obtained.
Nsurlconnection provides a class method to quickly send an asynchronous request
+ (NSData *) Sendsynchronousrequest: (nsurlrequest *) Request Returningresponse: (Nsurlresponse *) Response error: ( Nserror * *) error
Incoming Request: Requested object (URL, request type, request parameter)
Parameter response: Response object
Argument error: Wrong object (if an error occurs)
Return Value: Response data
such as: request to get a picture resource
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7C/B0/wKioL1bWYWXCkCajAAFpKulSO7c601.png "title=" screen shot 2016-03-02 a.m. 11.41.38.png "alt=" Wkiol1bwywxckcajaafpkulso7c601.png "/>
The drawbacks of synchronous requests:
The request process takes time, causes the current thread to block, and cannot perform other operations
Especially when called in the main thread, the app will not be able to perform any UI actions during this process.
That is, the operation of the synchronous request is placed in a child thread to execute
Note that the child thread cannot update the UI, and the update UI action needs to be completed in the main thread
Example: Using GCD
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7C/B2/wKiom1bWYSiRR1IWAAGvgpG5Ohs592.png "title=" screen shot 2016-03-02 a.m. 11.42.44.png "alt=" Wkiom1bwysirr1iwaagvgpg5ohs592.png "/>
Nsurlconnection provides a way to send an asynchronous request
+ (void) Sendasynchronousrequest: (nsurlrequest *) request queue: (Nsoperationqueue *) queue completionhandler: (void (^) ( Nsurlresponse *response, NSData *data, Nserror *connectionerror)) handler
Asynchronous operation, the current thread does not wait for the completion of the request process, so the result cannot be obtained by parameter or return value
operations that result from an asynchronous operation typically have several practices: proxy, notification, block
This operation uses block mode, and the parameters in the block are usually obtained (response object, response data, Error object)
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7C/B0/wKioL1bWYc_zMXchAAHEY_g9J-k704.png "title=" screen shot 2016-03-02 a.m. 11.43.31.png "alt=" Wkiol1bwyc_zmxchaahey_g9j-k704.png "/>
The Nsurlconnection object contains a proxy property
Provide agency agreement <NSURLConnectionDelegate><NSURLConnectionDataDelegate>
Common Proxy methods:
Received response object-(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response//received some or all of the data- (void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data//Complete receipt of response data-(void) Connectiondidfinishloading: (nsurlconnection *) connection//request failed-(void) connection: (Nsurlconnection *) connection Didfailwitherror: (nserror*) error
Such as:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7C/B2/wKiom1bWYZHhlNtYAAJJBxcwkZU631.png "title=" screen shot 2016-03-02 a.m. 11.44.27.png "alt=" Wkiom1bwyzhhlntyaajjbxcwkzu631.png "/>
This article is from the "Ammon" blog, make sure to keep this source http://annmeng.blog.51cto.com/3321237/1746697
iOS Web Development (2) using nsurlconnection