IOS network with multithreaded--3. Network request with asynchronous get mode (non-blocking)

Source: Internet
Author: User

By getting the network data asynchronously, the asynchronous request does not block the main thread (the user interface does not die) and a new thread is created.

The code is as follows

ViewController.h file

1 //2 //ViewController.h3 //Appdemo4 //5 //Created by Jinxin on 15/12/2.6 //copyright©2015 year xx. All rights reserved.7 //8 9 #import<UIKit/UIKit.h>Ten  One //1. Add a protocol agent A @interfaceViewcontroller:uiviewcontroller<nsurlconnectiondatadelegate> -  - //2. Add a property to accept the network data.  the@property (nonatomic) NSData *Receivedata; -  - @end

VIEWCONTROLLER.M file

1 //2 //VIEWCONTROLLER.M3 //Appdemo4 //5 //Created by Jinxin on 15/12/2.6 //copyright©2015 year xx. All rights reserved.7 //8 9 #import "ViewController.h"Ten  One  A @interfaceViewcontroller () -  - @end the  - @implementationViewcontroller -  -- (void) Viewdidload { + [Super Viewdidload]; -     //additional setup after loading the view, typically from a nib. +      A     //3. Create a URL object that specifies the URL of the requested data, this section calls the public API of Facebook to get an FB user information.  atNsurl *url = [Nsurl urlwithstring:@"HTTPS://GRAPH.FACEBOOK.COM/APPLE-INC"]; -     //4. Create a network Request object from the URL, -     //parameter 1: request access Path -     //Parameter 2: Cache protocol -     //Parameter 3: Network request time-out -Nsurlrequest *request = [[Nsurlrequest alloc] Initwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy timeOutInterval:Ten]; in     //5. Use network objects for network communication, create a network connection when the network Request object is created successfully.  -Nsurlconnection *connection = [[Nsurlconnection alloc] Initwithrequest:requestDelegate: self]; to      +      - } the  * //6. Add an agent method that executes this method when the network feedback is received.  $-(void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) ResponsePanax Notoginseng { -Self.receivedata =[Nsmutabledata data]; the } +  A //7. Add a proxy method that executes this method when the network data is received the-(void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) Data + { -Self.receivedata =data; $ } $  - //8. Add an agent method that executes this method when a series of actions for the network connection are completed.  --(void) Connectiondidfinishloading: (Nsurlconnection *) Connection the { -     //Converts the received network data, from the binary data format, to the string format, and outputs the resultWuyiNSString *receivestr =[[NSString alloc]initwithdata:self.receivedata encoding:nsutf8stringencoding]; theNSLog (@">>>>>>>>>>>>>>>>%@", receivestr); - } Wu  - //9. Add a proxy method when the network connection fails to execute this method About-(void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) Error $ { -NSLog (@">>>>>>>>>>>>>>>>%@", [Error localizeddescription]); - } -  A- (void) didreceivememorywarning { + [Super didreceivememorywarning]; the     //Dispose of any resources the can be recreated. - } $  the  the  the @end

After execution, the data can be obtained based on the specified URL.

IOS network with multithreaded--3. Network request with asynchronous get mode (non-blocking)

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.