Created
April 4, 2017 22:45
-
-
Save barezina/25c855f2fba0c48591d01952c89c3594 to your computer and use it in GitHub Desktop.
Singleton for making get-post requests (passing in external block, completion handler)
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
// | |
// singleton.m | |
// | |
#import "singleton.h" | |
typedef void(^ CompletionBlock)(NSData*, NSURLResponse*, NSError*); | |
@implementation singleton | |
@synthesize vData; | |
- (id)init { | |
if (self = [super init]) { | |
// The master object | |
vData = [[NSMutableDictionary alloc]init]; | |
} | |
return self; | |
} | |
+ (id) sharedSingleton { | |
static singleton *sharedSingleton = nil; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
sharedSingleton = [[self alloc] init]; | |
}); | |
return sharedSingleton; | |
} | |
- (void) performGetRequestWithURL: (NSURL *) url withCompletionHandler: (CompletionBlock) theBlock { | |
// GET SIMPLE: how to use this function from outside the singleton | |
/* | |
NSURL *url = [routes auth_authenticate]; | |
void (^theBlock) (NSData*, NSURLResponse*, NSError*) = ^(NSData *data, NSURLResponse *response, NSError *error) { | |
NSLog(@"Got response %@ with error %@.\n", response, error); | |
NSLog(@"DATA:\n%@\nEND DATA\n", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); | |
}; | |
[s performGetRequestWithURL:url withCompletionHandler:theBlock]; | |
*/ | |
NSLog(@"performGetRequest"); | |
NSURLSessionConfiguration *defaultConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; | |
NSURLSession *sessionWithoutADelegate = [NSURLSession sessionWithConfiguration:defaultConfiguration]; | |
[[sessionWithoutADelegate dataTaskWithURL:url completionHandler:theBlock] resume]; | |
} | |
- (void) performPostRequestWithURL:(NSURL *)url withDictionary:(NSMutableDictionary *)dictionary withCompletionHandler: (CompletionBlock) theBlock { | |
// POST SIMPLE: how to use this function from outside the singleton. | |
/* | |
NSURL *url = [routes auth_authenticate]; | |
NSMutableDictionary *dictionaryParameters = [[NSMutableDictionary alloc]init]; | |
NSString *email = [s.vData objectForKey:@"email"]; | |
NSString *password = [s.vData objectForKey:@"password"]; | |
[dictionaryParameters setValue:email forKey:@"email"]; | |
[dictionaryParameters setValue:password forKey:@"password"]; | |
void (^theBlock) (NSData*, NSURLResponse*, NSError*) = ^(NSData *data, NSURLResponse *response, NSError *error) { | |
singleton *s = [singleton sharedSingleton]; | |
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; | |
NSLog([JSON description]); | |
}; | |
[s performPostRequestWithURL:url withDictionary:dictionaryParameters withCompletionHandler:theBlock]; | |
*/ | |
NSError *error; | |
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; | |
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil]; | |
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; | |
[request setValue:@"application/json" forHTTPHeaderField:@"Content-type"]; | |
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; | |
[request setHTTPMethod:@"POST"]; | |
NSData *postData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error]; | |
[request setHTTPBody:postData]; | |
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:theBlock]; | |
[postDataTask resume]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment