Code implementation:
Implementation of the rootviewcontroller class:
View code
//// Rootviewcontroller. M // httprequestbyblocktest // Method 1: Implement callback through blocks-essentially a function pointer // Method 2: use the function pointer to implement callback // created by Hanjun on 13-3-15. // copyright (c) 2013 companyofhanjun. all rights reserved. // # import "rootviewcontroller. H "# import" httprequst. H "@ interface rootviewcontroller () @ end @ implementation rootviewcontrollervoid (* pfunc2) (nsdata * data);-(void) viewdidload {[Super viewdidload]; [self createcontr Ol];}-(void) createcontrol {httprequst * request = [[httprequst alloc] init]; // [Request requestdataformblock: ^ (nsdata * Data) {// method 1:1. use block syntax to send function pointers to network classes, request processing // nslog (@ "backdata: % @", data); // method. the message is sent. The callback is complete! //}]; Pfunc2 = getdata; // method 2:1. point the pfunc2 function pointer to the getdata function [Request requestdatafromfunctionpointer: pfunc2]; // method. pass the function pointer to the network processing class} // method. the Network Class calls this method, sends the message, and the callback ends! Void getdata (nsdata * Data) {nslog (@ "backdata: % @", data);}-(void) didreceivemorywarning {[Super didreceivemorywarning]; // dispose of any resources that can be recreated .} @ end
Httprequest class implementation:
View code
#import <Foundation/Foundation.h>@interface HttpRequst : NSObject<NSURLConnectionDataDelegate,NSURLConnectionDelegate>{ NSMutableData *_receiverData;}- (void)requestDataFormBlock:(void (^)(NSData * data))getData;- (void)requestDataFromFunctionPointer:(void (*)(NSData * data))getData;@end
View code
//// Httprequst. M // httprequestbyblocktest /// created by Hanjun on 13-3-15. // copyright (c) 2013 companyofhanjun. all rights reserved. // # import "httprequst. H "void (^ pfunc1) (nsdata * data); // method. define the block pointer void (* pfunc2) (nsdata * data) in the Network Class; // The method is. define the function pointer @ implementation httprequst-(ID) Init {self = [Super init]; If (Self) {_ receiverdata = [[nsmutabledata alloc] initwithcapacity in the network class: 100];} return self;} // method. the Network Class receives the request (including the block-function pointer of the message object) and starts processing-(void) requestdataformblock :( void (^) (nsdata * Data )) getdata {nsurl * url = [nsurl urlwithstring: @ "http://www.baidu.com"]; response * request = [required requestwithurl: URL cachepolicy: 0 timeoutinterval: 30.0]; [nsurlconnection connectionwithrequest: Request delegate: self]; pfunc1 = getdata; // method. let's point the custom pointer to the sent function pointer} // method. the Network Class receives a request (a function pointer containing the information object) and starts processing.-(void) requestdatafromfunctionpointer :( void (*) (nsdata * Data )) getdata {nsurl * url = [nsurl urlwithstring: @ "http://www.baidu.com"]; response * request = [required requestwithurl: URL cachepolicy: 0 timeoutinterval: 30.0]; [nsurlconnection connectionwithrequest: Request delegate: self]; pfunc2 = getdata; // method. let's point the custom pointer to the sent function pointer}-(void) connection :( nsurlconnection *) connection didreceiveresponse :( nsurlresponse *) response {}-(void) connection :( nsurlconnection *) connection didreceivedata :( nsdata *) Data {[_ receiverdata appenddata: Data];}-(void) connectiondidfinishloading :( nsurlconnection *) connection {// (pfunc1) (_ receiverdata ); // method. network Class processing is complete. Send the requested data to the function pointer parameter (pfunc2) (_ receiverdata); // method. network Class processing is complete. Send the requested data to the function pointer parameter}-(void) connection :( nsurlconnection *) connection didfailwitherror :( nserror *) error {} @ end
Block callback steps:
1. Send function pointers and request processing to network classes through block syntax;
2. Define the block pointer in the Network Class;
3. The network class receives the request (the block-function pointer containing the information object) and starts processing;
4. Let our custom pointer point to the function pointer sent;
5. After network processing is completed, send the requested data to the function pointer parameter;
6. Get the sent message. The callback is complete!
Function pointer callback steps:
1. Point the pfunc2 function pointer to the getdata function;
2. Pass the function pointer to the network processing class;
3. Define the function pointer in the Network Class;
4. The network class receives the request (including the function pointer of the information object) and starts processing;
5. Let's point the custom pointer to the function pointer sent;
6. After network processing is completed, send the requested data to the function pointer parameter;
7. The Network Class calls this method, sends the message, and the callback ends!