Skip to content

Instantly share code, notes, and snippets.

@chriswebb09
Created December 30, 2016 10:50
Show Gist options
  • Save chriswebb09/a63f1f70d076fc3b23f73ae19db47d42 to your computer and use it in GitHub Desktop.
Save chriswebb09/a63f1f70d076fc3b23f73ae19db47d42 to your computer and use it in GitHub Desktop.
Objective-C APIClient
#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
#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