iOS Network--nsurlconnection

Source: Internet
Author: User

¥ display of the Rotate load icon in the status bar

[UIApplication sharedapplication].networkactivityindicatorvisible = YES;

Cancel to stop the download

1, the URL is again packaged into a Nsurlrequest object, can be used by nsurlconnection

1. nsurlrequest * request = [nsurlrequest Requestwithurl:url];

2. Nsurlrequestreloadignoringlocalcachedata Ignore local cache

timeOutInterval maximum time to wait for a request

Nsurlrequest *request = [Nsurlrequest requestwithurl:url cachepolicy:nsurlrequestreloadignoringlocalcachedata TIMEOUTINTERVAL:20];

3. Request a file of the specified size, get the size of the current download, and encapsulate it as a range

NSString * rangestr = [NSString stringwithformat:@ "bytes=%lld-", [Self.downloadsize Longlongvalue]];

set the request data range (method Forhttpheaderfield in Nsmutableurlrequest)

[Request Setvalue:rangestr forhttpheaderfield:@ "Range"];

1. Establish network link according to request nsurlconnection

Divided into three ways:

(1) Sync request

Synchronization requests are generally not required because the card UI, but certain circumstances, may be used, the bank guarantees security, the game updates

1 "nsurlconnection Establish network connection (synchronous)

NSData * data = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error];

2 "After the request to determine whether the request is successful

if (Error) {

NSLog (@ "%@", error);

return;

}

(2) Asynchronous request (block)

Block-form Asynchronous request

1. Simple operation, return the whole data

2. Cannot be received in step, cannot show progress

1 Send asynchronous request

Parameter one: queue, get home row

return value: 1. Nsurlresponse *response

2. NSData *data

3. Nserror *connectionerror

[Nsurlconnection sendasynchronousrequest:request queue:mainqueue completionhandler:^ (NSURLResponse *response, NSData *data, Nserror *connectionerror) {--blcok callback, Download complete--}

2 "To determine the success of the download?" (Two different ways)

1. Judging by Status code

Nshttpurlresponse * Httpreponse = (Nshttpurlresponse *) response;

if (httpreponse.statuscode! = 200) {

NSLog (@ "Download error");

}

2. The data exists && error is empty

if (data &&!connectionerror) {

Self.imageView.image = [UIImage imagewithdata:data];

}else{

NSLog (@ "Download error");

}

(2) Asynchronous request (proxy)

Compliance Agreement Nsurlconnectiondatadelegate

4 Proxy methods

Ability to monitor download progress in real time

1. Erase your data before requesting

self.resultData.length = 0;

2 "Proxy method for asynchronous requests

[Nsurlconnection connectionwithrequest:request delegate:self];

3 "Four ways to implement a proxy

1. Start the download, the link is responding, the response header has been downloaded

-(void) Connection:didreceiveresponse:

Get the total length of the data

self.totalsize = response.expectedcontentlength;

2. Receiving data, receiving the response body, according to the data size, repeated uninterrupted reception

-(void) Connection:didreceivedata:

Uninterrupted reception of data, stitching into global variables

[Self.resultdata Appenddata:data];

Get progress ratio

float PV = self.resultdata.length/self.totalsize;

Update the progress bar, asynchronous, (some UI action to use async)

Dispatch_async (Dispatch_get_main_queue (), ^{

Self.progressView.progress = PV;

});

3 Download complete

-(void) Connectiondidfinishloading:

4. Download error occurred

1. Disconnected the network

2. Address Error

-(void) Connection:didfailwitherror:

Summary: 4ge proxy method

(1) Ready to download, link and respond, have finished downloading the response header Didreceiveresponse

Get the total size of a file

(2) downloading, repeatedly calling Didreceivedata

Update download Progress

(3) Download complete connectiondidfinishloading

Release the object, assign the downloaded data to where it needs to be displayed

(4) Download failed Didfailwitherror

Releasing objects, printing error messages

iOS Network--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.