iOS Web Development (2) using nsurlconnection

Source: Internet
Author: User

    • Nsurlconnection Introduction

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.



    • Network requests that are synchronized

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.



    • Asynchronous network requests-multi-threaded mates

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 "/>


    • Asynchronous network request-Send asynchronous Request method

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 "/>


    • Asynchronous network request-use proxy to listen for request process

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.