Wednesday, 5 August 2015

NSURLConnection using NSObject


ServiceConnector.h

#import <Foundation/Foundation.h>

@protocol ServiceConnectorDelegate <NSObject>

-(void)requestReturnedData:(NSMutableData*)data;
-(void)requestReturnedError:(NSString*)error;

@end

@interface ServiceConnector : NSObject <NSURLConnectionDelegate,NSURLConnectionDataDelegate>

@property (strong,nonatomic) id <ServiceConnectorDelegate> delegate;

- (void)sendRequest:(NSString*)urlStr postValue:(NSString*)postStr;

@end

ServiceConnector.m

#import "ServiceConnector.h"

@implementation ServiceConnector{
    NSMutableData *receivedData;
}

- (void)sendRequest:(NSString*)urlStr postValue:(NSString*)postStr
{
    NSMutableString *post = [NSMutableString stringWithFormat:@"%@",postStr];
    
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",kBaseURL,urlStr]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                       timeoutInterval:10.0];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
    
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
    if (connection) {
        receivedData = nil;
    }
}
#pragma mark - Data connection delegate -
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    receivedData = [[NSMutableData alloc]init];
    
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [receivedData appendData:data];
    
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [self.delegate requestReturnedError:@"noInternet"];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    
    [self.delegate requestReturnedData:receivedData];//send the data to the delegate
}

@end

example:

#import "ServiceConnector.h"
@interface HomeViewController : UIViewController<ServiceConnectorDelegate>

- (void)viewDidLoad
{

    [super viewDidLoad];

ServiceConnector *serviceConnector = [[ServiceConnector alloc] init];
        serviceConnector.delegate = self;
        NSString *postValue = [NSString stringWithFormat:@"sUser_id=%@&simage_id=%@",[self.sUser_id description],deleteId];
        [serviceConnector sendRequest:@"url.php" postValue:postValue];
}

-(void)requestReturnedError:(NSString *)error{
      NSLog(@"No internet connection");
}

-(void)requestReturnedData:(NSMutableData *)data{ 
//activated when data is returned    
    id jsonObjects = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
}

No comments:

Post a Comment