I\'m having an issue when i\'m trying to send JSON request with an iOS app. I have to send something like this :
http://url/User/Create/{\"newsletter\" : 1,\"pa
HttpRequestSenderDelegate.h
@protocol HttpRequestSenderDelegate
-(void) didFinishLoading:(BOOL)success data:(NSData*)data;
@end
HttpPostRequestSender.h
@protocol HttpRequestSenderDelegate;
@interface HttpPostRequestSender : NSObject
- (id)initWithDelegate:(id) delegate;
- (void)send:(NSString*)url postParams:(NSString*)postParams;
- (void)cancelDownload;
@end
HttpPostRequestSender.m
#import "HttpPostRequestSender.h"
#import "HttpRequestSenderDelegate.h"
@interface HttpPostRequestSender ()
@property (nonatomic, weak) id delegate;
@property (nonatomic, strong) NSMutableData *activeDownload;
@property (nonatomic, strong) NSURLConnection *dataConnection;
@end
@implementation HttpPostRequestSender
@synthesize delegate = _delegate,
activeDownload = _activeDownload,
dataConnection = _dataConnection;
- (id)initWithDelegate:(id) delegate
{
self = [super init];
if (self) {
self.delegate = delegate;
}
return self;
}
- (void)cancelDownload
{
[self.dataConnection cancel];
self.dataConnection = nil;
self.activeDownload = nil;
}
- (void)send:(NSString*)url postParams:(NSString*)postParams
{
NSURL *nsurl = [NSURL URLWithString:url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nsurl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[postParams dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.dataConnection = conn;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.activeDownload = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.activeDownload appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[self.delegate didFinishLoading:YES data:self.activeDownload];
self.dataConnection = nil;
self.activeDownload = nil;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The download could not complete - please make sure you're connected to either 3G or Wi-Fi." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[errorView show];
[self.delegate didFinishLoading:NO data:nil];
self.dataConnection = nil;
self.activeDownload = nil;
}
@end
in your view controller : MyViewController.h
#import
#import "HttpRequestSenderDelegate.h"
@interface MyViewController : UIViewController
@end
MyViewController.m
#import "MyViewController.h"
#import "HttpPostRequestSender.h"
@interface MyViewController ()
@property (nonatomic, strong) HttpPostRequestSender *requestSender;
@end
@implementation MyViewController
@synthesize requestSender = _requestSender;
-(HttpPostRequestSender*) requestSender
{
if(_requestSender == nil) _requestSender = [[HttpPostRequestSender alloc] initWithDelegate:self];
return _requestSender;
}
- (void)viewDidLoad
{
//for example i send the request in the view did load
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSString *url = @"your url";
//post params
NSString *postParams =@"login=sdsd&password=sdsd&email=sdsd@hotmail.fr";
[self.requestSender send:url postParams:postParams];
}
- (void) didFinishLoading:(BOOL)success data:(NSData *)data
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if(success && data != nil) {
//here you can parse the response
}
}
@end