Last active
December 30, 2015 04:48
-
-
Save jchudzynski/7777915 to your computer and use it in GitHub Desktop.
Contains methods that are making networking calls and parse the response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
@class WebServicesHelper | |
@abstract | |
Contains methods that are making networking calls and parse the response | |
@discussion | |
*/ | |
#import "WebServicesHelper.h" | |
#define WEB_API_URL @"http://djmobilesoftware.com/validjson.json" | |
typedef void (^SuccessBlock)(NSData *); | |
typedef void (^ErrorBlock)(NSError *); | |
@interface WebServicesHelper()<NSURLConnectionDelegate, NSURLConnectionDataDelegate> | |
@property(nonatomic,strong) NSURLConnection * connection; | |
@property (nonatomic,copy) SuccessBlock successBlock; | |
@property (nonatomic,copy) ErrorBlock errorBlock; | |
@property (nonatomic,strong) NSMutableData * data; | |
@end | |
@implementation WebServicesHelper | |
-(instancetype)init{ | |
if(self =[super init]) | |
{ | |
NSURL * url = [NSURL URLWithString:WEB_API_URL]; | |
NSURLRequest * request = [NSURLRequest requestWithURL:url]; | |
_connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; | |
_data = [NSMutableData new]; | |
} | |
return self; | |
} | |
-(void)getDataWithCompletionBlock:(void (^)(NSData * data)) completion andErrorBlock:(void (^)(NSError *))errorBlock{ | |
self.successBlock = [completion copy]; | |
self.errorBlock = [errorBlock copy]; | |
[_connection start]; | |
} | |
#pragma mark NSURLConnection Delegate | |
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ | |
NSLog(@"Error! %@",error.debugDescription); | |
self.errorBlock(error); | |
} | |
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{ | |
self.successBlock(_data); | |
NSLog(@"Finish Loading Data"); | |
} | |
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ | |
[_data appendData:data]; | |
NSLog(@"Appending Data"); | |
} | |
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ | |
NSLog(@"Response %@",response); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment