You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.9 KiB
79 lines
2.9 KiB
9 years ago
|
//
|
||
|
// CLAPI.m
|
||
|
// Clocker
|
||
|
//
|
||
|
// Created by Abhishek Banthia on 1/10/16.
|
||
|
//
|
||
|
//
|
||
|
|
||
8 years ago
|
#import "CLAPIConnector.h"
|
||
9 years ago
|
#import "Reachability.h"
|
||
|
|
||
8 years ago
|
@implementation CLAPIConnector
|
||
9 years ago
|
|
||
|
+ (void)dataTaskWithServicePath:(NSString *)path
|
||
|
bySender:(id)sender
|
||
|
withCompletionBlock:(void (^)(NSError *error, NSDictionary *dictionary))completionBlock
|
||
|
{
|
||
|
|
||
8 years ago
|
__block NSDictionary *responseDictionary = @{};
|
||
9 years ago
|
|
||
|
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
|
||
|
|
||
|
sessionConfig.timeoutIntervalForRequest = 20;
|
||
|
|
||
|
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];
|
||
|
|
||
|
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", path]];
|
||
|
|
||
|
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
|
||
8 years ago
|
request.HTTPMethod = @"GET";
|
||
9 years ago
|
|
||
|
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
|
||
|
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
|
||
|
|
||
|
NSURLSessionDataTask *dataTask = [session
|
||
|
dataTaskWithRequest:request
|
||
|
completionHandler:^(NSData *data,
|
||
|
NSURLResponse *response,
|
||
|
NSError *error)
|
||
|
{
|
||
|
|
||
|
/*Check if any error. If nil then proceed*/
|
||
|
if (error == nil)
|
||
|
{
|
||
|
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
|
||
|
if (httpResp.statusCode == 200)
|
||
|
{
|
||
|
responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
|
||
|
completionBlock(error, responseDictionary);
|
||
|
}
|
||
|
}
|
||
|
/*Error is not nil. Show error*/
|
||
|
else
|
||
|
{
|
||
|
completionBlock(error, nil);
|
||
|
|
||
|
}
|
||
|
}];
|
||
|
|
||
|
[dataTask resume];
|
||
|
|
||
|
}
|
||
|
|
||
|
+ (BOOL)isUserConnectedToInternet
|
||
|
{
|
||
|
Reachability *reachability = [Reachability reachabilityForInternetConnection];
|
||
|
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
|
||
|
|
||
|
if (networkStatus == NotReachable)
|
||
|
{
|
||
|
return NO;
|
||
|
}
|
||
|
|
||
|
return YES;
|
||
|
}
|
||
|
|
||
|
|
||
|
@end
|