Created
December 30, 2016 10:50
-
-
Save chriswebb09/a63f1f70d076fc3b23f73ae19db47d42 to your computer and use it in GitHub Desktop.
Objective-C APIClient
This file contains hidden or 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
#import <Foundation/Foundation.h> | |
@interface APIClient : NSObject | |
@property(strong,nonatomic) NSString *urlString; | |
@property(strong, nonatomic) NSURL *url; | |
@property(strong, nonatomic) NSURLSessionDataTask *downloadTask; | |
-(void)sendAPICallWith:(void (^)(NSDictionary * dictionary))completionHandler; | |
-(id)initWithURLString:(NSString *)url; | |
@end |
This file contains hidden or 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
#import "APIClient.h" | |
@implementation APIClient | |
- (id) init { | |
if (self = [super init]) { | |
return self; | |
} else { | |
return nil; | |
} | |
} | |
-(id)initWithURLString:(NSString *)url { | |
self.urlString = url; | |
return [self init]; | |
} | |
-(void)sendAPICallWith:(void (^)(NSDictionary * dictionary))completionHandler { | |
self.url = [NSURL URLWithString:self.urlString]; | |
NSURLSession *session = [NSURLSession sharedSession]; | |
self.downloadTask = [session dataTaskWithURL: self.url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { | |
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; | |
if (completionHandler) { | |
completionHandler(json); | |
} | |
}]; | |
[self.downloadTask resume]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment