Advertisement
Guest User

ObjcClass

a guest
Apr 26th, 2012
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // WebUtil.h:
  2.  
  3. #import <Foundation/Foundation.h>
  4.  
  5. @interface WebUtil : NSObject {
  6.     NSMutableData *responseData;
  7. }
  8. @property (nonatomic, retain) NSMutableData *responseData;
  9. - (void) performRequest;
  10. @end
  11.  
  12. // WebUtil.m
  13.  
  14. #import "WebUtil.h"
  15.  
  16. @implementation WebUtil
  17.  
  18. @synthesize responseData;
  19.  
  20. - (void) performRequest
  21. {
  22.     // create request
  23.     NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]
  24.                                                 cachePolicy:NSURLRequestUseProtocolCachePolicy
  25.                                             timeoutInterval:60.0];
  26.                                                              
  27.                                
  28.                                
  29. //    // create the connection for the request
  30.     NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  31.      
  32.     if (theConnection)
  33.     {
  34.         // create NSMutableData to hold the data
  35.         responseData = [[NSMutableData alloc] retain];
  36.     }
  37.    
  38.     return;
  39. }
  40.  
  41. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
  42. {
  43.     // null out response, ready to receive
  44.     [responseData setLength:0];
  45. }
  46.  
  47. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  48. {
  49.     [responseData appendData:data];
  50. }
  51.  
  52. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
  53. {
  54.     NSLog(@"Failed nsurl");
  55. }
  56.  
  57. - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  58. {
  59.     NSLog([NSString stringWithUTF8String:[responseData bytes]] );
  60.     [connection release];
  61. }
  62.  
  63.  
  64. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement