Guest User

AbstractURLConnection

a guest
Mar 21st, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.03 KB | None | 0 0
  1. //
  2. //  AbstractNSUrlConnection.h
  3. //
  4. #import <Foundation/Foundation.h>
  5.  
  6. @interface AbstractNSUrlConnection : NSObject <NSURLConnectionDelegate>{
  7.    
  8.     // used to append the received data to
  9.     NSMutableData *receivedData;
  10.     // the connection that we are delegate of
  11.     NSURLConnection *theConnection;
  12.     // the object id of the caller
  13.     id sender;
  14.     // the callback method of the caller
  15.     NSString *selector;
  16. }
  17.  
  18. /*
  19.  Properties for the local variables.
  20. */
  21. @property (strong, retain) NSURLConnection * theConnection;
  22. @property (strong, retain) NSString *selector;
  23. @property (strong, retain) id sender;
  24.  
  25. // returns self and sets up the connection, caller and callback
  26. - (AbstractNSUrlConnection *) initWithRequest:(NSURLRequest *) url sender:(id) sender withSelector: (NSString *)selector andRequestMethod:(NSString *) method;
  27.  
  28. /*
  29.  Class methods to get different request types.
  30. */
  31. + (NSString *) MethodTypePOST;
  32. + (NSString *) MethodTypeGET;
  33. + (NSString *) MethodTypePUT;
  34. + (NSString *) MethodTypeREQUEST;
  35.  
  36. @end
  37.  
  38. #import "AbstractNSUrlConnection.h"
  39.  
  40. /*
  41.     different request types
  42.  */
  43. #define METHOD_TYPE_POST @"POST"
  44. #define METHOD_TYPE_PUT @"PUT"
  45. #define METHOD_TYPE_GET @"GET"
  46. #define METHOD_TYPE_REQUEST @"REQUEST"
  47.  
  48. @implementation AbstractNSUrlConnection
  49.  
  50. @synthesize theConnection, sender, selector;
  51.  
  52. /*
  53.  This methods sets up the NSURLConneciton and tells it we're it's
  54.  delegate. It also takes an url, a sender, a callback and initialises
  55.  the received data array.
  56. */
  57. - (AbstractNSUrlConnection *) initWithRequest:(NSURLRequest *)url
  58.         sender:(id)theSender
  59.         withSelector:(NSString *)theSelector
  60.         andRequestMethod:(NSString *) method {
  61.    
  62.     // init ourself
  63.     self = [super init];
  64.     // allocate memory for the connection, give it an url and tell it we're delegate
  65.     theConnection = [[NSURLConnection alloc] initWithRequest:url delegate:self];
  66.     // hold the received data
  67.     receivedData = [[NSMutableData alloc] init];
  68.     // set the sender
  69.     self.sender = theSender;
  70.     // set the selector
  71.     self.selector = theSelector;
  72.    
  73.     return self;
  74. }
  75.  
  76. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
  77. {
  78.     // This method is called when the server has determined that it
  79.     // has enough information to create the NSURLResponse.
  80.    
  81.     // It can be called multiple times, for example in the case of a
  82.     // redirect, so each time we reset the data.
  83.    
  84.     // receivedData is an instance variable declared elsewhere.
  85.     [receivedData setLength:0];
  86. }
  87.  
  88. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  89. {
  90.     // Append the new data to receivedData.
  91.     // receivedData is an instance variable declared elsewhere.
  92.     [receivedData appendData:data];
  93. }
  94.  
  95. - (void)connection:(NSURLConnection *)connection
  96.   didFailWithError:(NSError *)error
  97. {
  98.     // inform the user
  99.     NSLog(@"Connection failed! Error - %@ %@",
  100.           [error localizedDescription],
  101.           [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
  102. }
  103.  
  104. - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  105. {
  106.     NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
  107.    
  108.     // see if the sender responds to the selector befire we actually
  109.     // call it.
  110.     if([sender respondsToSelector:NSSelectorFromString(selector)] && self.sender) {
  111.         // call the sender (callback)
  112.         [self.sender performSelector: NSSelectorFromString(selector)];
  113.        
  114.         /*
  115.          we don't need thease anymore...
  116.         */
  117.         receivedData = nil;
  118.         theConnection = nil;
  119.         sender = nil;
  120.         selector = nil;
  121.     }
  122. }
  123.  
  124. /*
  125.  Class methods to get different request types.
  126. */
  127.  
  128. + (NSString *) MethodTypePOST{
  129.     return METHOD_TYPE_POST;
  130. }
  131.  
  132. + (NSString *) MethodTypeGET{
  133.     return METHOD_TYPE_GET;
  134. }
  135.  
  136. + (NSString *) MethodTypePUT{
  137.     return METHOD_TYPE_POST;
  138. }
  139.  
  140. + (NSString *) MethodTypeREQUEST{
  141.     return METHOD_TYPE_REQUEST;
  142. }
  143.  
  144. @end
Advertisement
Add Comment
Please, Sign In to add comment