NXWebAccess.h
#import "Foundation/Foundation.h"
#import "SBJSON.h"
@interface NXWebAccess : NSObject {
id delegate_;
NSMutableData* data_;
NSNumber* responseCode_;
SEL callback_;
NSError *webAccessError_;
int webAccessResponseCode_;
NSData *webAccessResult_;
}
@property (nonatomic, assign) id delegate;
@property (nonatomic, assign) SEL callback;
@property (nonatomic, retain) NSMutableData* data;
@property (nonatomic, retain)NSError* webAccessError;
@property (nonatomic, assign)int webAccessResponseCode;
@property (nonatomic, retain)NSData* webAccessResult;
+ (NXWebAccess*)sharedWebAccessWithDelegate:(id)delegate withSelector:(SEL)selector;
- (BOOL)asyncRequestWithURL:(NSString*)urlString withMethod:(NSString*)method withData:(NSString*)dataString;
//-
(void)getDataAsync:(NSString *)url withMethod:(NSString *)method
withData:(NSString *)data
// withDelegate:(id)delegate
withSelector:(SEL)selector;
+ (NXWebAccess
*)syncRequestWithURL:(NSString*)url withMethod:(NSString*)method withData:(NSString *)dataString;
@end
NXWebAccess.m
#import "NXWebAccess.h"
#import "NXAppConfiguration.h"
static NXWebAccess *sharedWebAccess = nil;
@implementation NXWebAccess
@synthesize delegate = delegate_;
@synthesize callback = callback_;
@synthesize data = data_;
@synthesize webAccessError = webAccessError_;
@synthesize webAccessResponseCode = webAccessResponseCode_;
@synthesize webAccessResult = webAccessResult_;
- (void)dealloc
{
[data_ release];
[webAccessError_ release];
[webAccessResult_ release];
[super dealloc];
}
+ (NXWebAccess*)sharedWebAccessWithDelegate:(id)delegate withSelector:(SEL)selector
{
@synchronized(self) {
if (sharedWebAccess == nil) {
sharedWebAccess = [[self alloc] init];
}
sharedWebAccess.callback = selector;
sharedWebAccess.delegate = delegate;
}
return sharedWebAccess;
}
//-
(void)getDataAsync:(NSString *)url withMethod:(NSString *)method
withData:(NSString *)data
// withDelegate:(id)delegate
withSelector:(SEL)selector
//{
//// self.url = url;
// self.delegate = delegate;
// self.callback = selector;
// [self requestWithURL:url withData:data
withMethod:method];
//
//}
- (BOOL)asyncRequestWithURL:(NSString*)urlString withMethod:(NSString*)method withData:(NSString*)dataString
{
NSLog(@"NXWebAcccess -
urlString",urlString);
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
if ([method isEqualToString:@"POST"] || [method isEqualToString:@"PUT"]) {
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"%@",dataString] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
}
[request setHTTPMethod:method];
//Set UUID in header for the purpose of loging reqests
[request setValue:[[NXAppConfiguration sharedConfig] uuid] forHTTPHeaderField:@"UUID"];
self.data = [[NSMutableData data] retain];
NSURLConnection *urlConnection = [NSURLConnection connectionWithRequest:request delegate:self];
if (urlConnection == nil) {
return NO;
} else {
return YES;
}
}
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data
{
[data_ appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//if
(callback_ != nil) {
[delegate_ performSelector:callback_ withObject:data_ withObject:responseCode_];
//}
// [data_ release];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
NSLog(@"responseCode
@@@@@=== %d ",responseCode_ );
// if (callback_ != nil) {
[delegate_ performSelector:callback_ withObject:nil withObject:responseCode_];
// }
// [data_ release];
}
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
responseCode_ = [[NSNumber numberWithInteger:[(NSHTTPURLResponse *)response statusCode]] retain];
}
#pragma mark -
#pragma mark Synchronous
method
//If URL request get
Synchronous method, call to the this method
+ (NXWebAccess
*)syncRequestWithURL:(NSString*)url withMethod:(NSString*)method withData:(NSString *)dataString
{
NXWebAccess *webAccessSync = [[NXWebAccess alloc]init];
NSError *syncError = nil;
int syncResponseCode;
NSData *syncResult;
NSHTTPURLResponse
*response;
NSURL *urlType = [NSURL URLWithString:url];
NSMutableURLRequest
*request = [NSMutableURLRequest requestWithURL:urlType];
if ([method isEqualToString:@"POST"]
|| [method isEqualToString:@"PUT"]) {
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"%@",dataString] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
}
[request
setHTTPMethod:method];
NSData *syncResponse = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:&syncError];
syncResponseCode
= [response statusCode];
syncResult
= syncResponse;
if (syncResponseCode == 200) { // Response OK
webAccessSync.webAccessResponseCode
= syncResponseCode ;
webAccessSync.webAccessResult = syncResult
;
webAccessSync.webAccessError =syncError;
}else { // If the connection fails and retuns a error code
webAccessSync.webAccessResult = (NSData *)@"Bad Request"
;
webAccessSync.webAccessResponseCode
= 400;
webAccessSync.webAccessError =syncError;
}
return [webAccessSync autorelease];
}
@end