Use block for network request callbacks in iOS

Source: Internet
Author: User

Use block for network request callbacks in iOS

HttpRequest.h

  httprequest.h//  useblockcallback////  Created by Michael on 2/13/14.//  Copyright (c) EIMS. All rights reserved.//#import <foundation/foundation.h>typedef Void (^finishblock) (NSString *dataString); Interface Httprequest:nsobject<nsurlconnectiondatadelegate> @property (strong, nonatomic) Nsmutabledata * Resultdata; @property (Strong, Nonatomic) Finishblock finishblock;+ (void) Postrequestwithurl: (NSString *) urlStr                 Paramters: (NSString *) paramters              finshedblock: (finishblock) block; @end

  Httprequest.m

httprequest.m//useblockcallback////Created by Michael on 2/13/14.//Copyright (c) EIMS. All rights reserved.//#import "HttpRequest.h" @implementation httprequest+ (void) Postrequestwithurl: (NSString *) Urlstr paramters: (NSString *) paramters Finshedblock: (finishblock) block{HttpRequest *HTTPR    Equest = [[HttpRequest alloc]init];        Httprequest.finishblock = Block;    Nsurl *url = [Nsurl urlwithstring:urlstr]; Nsmutableurlrequest *requset = [[Nsmutableurlrequest alloc]initwithurl:url CachePolicy:    Nsurlrequestuseprotocolcachepolicy timeoutinterval:30];        [Requset sethttpbody:[paramters datausingencoding:nsutf8stringencoding];    Nsurlconnection *connection = [[Nsurlconnection alloc]initwithrequest:requset delegate:httprequest]; NSLog (Connection @ "Connection creation succeeded": @ "Connection creation failed");} /** * Callback */-(void) connection when receiving the server response: (nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{NS Httpurlresponse *httpresponse = (nshttpurlResponse *) Response;    if (!self.resultdata) {self.resultdata = [[Nsmutabledata alloc]init];    } else {[Self.resultdata setlength:0]; } if ([Response respondstoselector: @selector (allheaderfields)]) {nsdictionary *dic = [HttpResponse allheade        Rfields];    NSLog (@ "[network]allheaderfields:%@", [dic description]); }}/** * Called when the server transmits data, this method performs several */-(void) connection depending on the data size: (nsurlconnection *) connection didreceivedata: (NSData *) data{[Self.resultdata appenddata:data];}  /** * This method is called after the data is passed */-(void) connectiondidfinishloading: (nsurlconnection *) connection{nsstring *resultstr = [[NSString]    Alloc]initwithdata:self.resultdata encoding:nsutf8stringencoding];    if (self.finishblock) {self.finishblock (RESULTSTR); }}/** * During a network request, any errors (network disconnection, connection timeouts, etc.) will enter this method */-(void) connection: (Nsurlconnection *) connection didfailwitherror: (Nserror * ) error{NSLog (@ "Network error:%@", [Error Localizeddescription]);    if (self.finishblock) {self.finishblock (nil); }} @end

  Called in Viewcontroller:

[HttpRequest postrequestwithurl:@ "http://www.baidu.com"                          paramters:@ ""                       finshedblock:^ (NSString * datastring) {                           NSLog (@ "Finish callback block, Result:%@", datastring);}];

  

Use block for network request callbacks in iOS

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.