Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // AbstractNSUrlConnection.h
- //
- #import <Foundation/Foundation.h>
- @interface AbstractNSUrlConnection : NSObject <NSURLConnectionDelegate>{
- // used to append the received data to
- NSMutableData *receivedData;
- // the connection that we are delegate of
- NSURLConnection *theConnection;
- // the object id of the caller
- id sender;
- // the callback method of the caller
- NSString *selector;
- }
- /*
- Properties for the local variables.
- */
- @property (strong, retain) NSURLConnection * theConnection;
- @property (strong, retain) NSString *selector;
- @property (strong, retain) id sender;
- // returns self and sets up the connection, caller and callback
- - (AbstractNSUrlConnection *) initWithRequest:(NSURLRequest *) url sender:(id) sender withSelector: (NSString *)selector andRequestMethod:(NSString *) method;
- /*
- Class methods to get different request types.
- */
- + (NSString *) MethodTypePOST;
- + (NSString *) MethodTypeGET;
- + (NSString *) MethodTypePUT;
- + (NSString *) MethodTypeREQUEST;
- @end
- #import "AbstractNSUrlConnection.h"
- /*
- different request types
- */
- #define METHOD_TYPE_POST @"POST"
- #define METHOD_TYPE_PUT @"PUT"
- #define METHOD_TYPE_GET @"GET"
- #define METHOD_TYPE_REQUEST @"REQUEST"
- @implementation AbstractNSUrlConnection
- @synthesize theConnection, sender, selector;
- /*
- This methods sets up the NSURLConneciton and tells it we're it's
- delegate. It also takes an url, a sender, a callback and initialises
- the received data array.
- */
- - (AbstractNSUrlConnection *) initWithRequest:(NSURLRequest *)url
- sender:(id)theSender
- withSelector:(NSString *)theSelector
- andRequestMethod:(NSString *) method {
- // init ourself
- self = [super init];
- // allocate memory for the connection, give it an url and tell it we're delegate
- theConnection = [[NSURLConnection alloc] initWithRequest:url delegate:self];
- // hold the received data
- receivedData = [[NSMutableData alloc] init];
- // set the sender
- self.sender = theSender;
- // set the selector
- self.selector = theSelector;
- return self;
- }
- - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- {
- // This method is called when the server has determined that it
- // has enough information to create the NSURLResponse.
- // It can be called multiple times, for example in the case of a
- // redirect, so each time we reset the data.
- // receivedData is an instance variable declared elsewhere.
- [receivedData setLength:0];
- }
- - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- {
- // Append the new data to receivedData.
- // receivedData is an instance variable declared elsewhere.
- [receivedData appendData:data];
- }
- - (void)connection:(NSURLConnection *)connection
- didFailWithError:(NSError *)error
- {
- // inform the user
- NSLog(@"Connection failed! Error - %@ %@",
- [error localizedDescription],
- [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
- }
- - (void)connectionDidFinishLoading:(NSURLConnection *)connection
- {
- NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
- // see if the sender responds to the selector befire we actually
- // call it.
- if([sender respondsToSelector:NSSelectorFromString(selector)] && self.sender) {
- // call the sender (callback)
- [self.sender performSelector: NSSelectorFromString(selector)];
- /*
- we don't need thease anymore...
- */
- receivedData = nil;
- theConnection = nil;
- sender = nil;
- selector = nil;
- }
- }
- /*
- Class methods to get different request types.
- */
- + (NSString *) MethodTypePOST{
- return METHOD_TYPE_POST;
- }
- + (NSString *) MethodTypeGET{
- return METHOD_TYPE_GET;
- }
- + (NSString *) MethodTypePUT{
- return METHOD_TYPE_POST;
- }
- + (NSString *) MethodTypeREQUEST{
- return METHOD_TYPE_REQUEST;
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment