Created
March 3, 2014 06:31
-
-
Save sag333ar/9319503 to your computer and use it in GitHub Desktop.
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 "SessionDelegate.h" | |
#import "SessionConfiguration.h" | |
@interface SessionConfiguration () | |
+ (NSURLSessionConfiguration*)sessionConfig; | |
@end | |
@interface SessionDelegate () | |
+ (SessionDelegate*)sharedSessionDelegate; | |
@property (nonatomic, strong) NSMutableDictionary *dForData; | |
@property (nonatomic, strong) NSOperationQueue *opQ; | |
@end | |
static NSURLSession *sharedSession; | |
static SessionDelegate *sessionDelegate; | |
@implementation SessionDelegate | |
#pragma mark - initialization | |
+ (void)initialize { | |
sessionDelegate = [[SessionDelegate alloc] init]; | |
sharedSession = [NSURLSession sessionWithConfiguration:[SessionConfiguration sessionConfig] | |
delegate:sessionDelegate | |
delegateQueue:[NSOperationQueue mainQueue]]; | |
} | |
+ (SessionDelegate*)sharedSessionDelegate { | |
return sessionDelegate; | |
} | |
#pragma mark - Initialization | |
- (id) init { | |
if (self = [super init] ) { | |
self.dForData = [NSMutableDictionary dictionary]; | |
if(!(IS_IOS_7)) { | |
NSOperationQueue *myQueue = [[NSOperationQueue alloc] init]; | |
myQueue.name = @"APIQueue"; | |
myQueue.MaxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount; | |
self.opQ = myQueue; | |
} | |
} | |
return self; | |
} | |
#pragma mark - Requests | |
+ (void)invokeAPIRequest:(NSMutableURLRequest*)request completionHandler:(void (^)(NSData* data, NSError *error))completionHandler { | |
if(IS_IOS_7) { | |
NSURLSessionDataTask *task = [sharedSession dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { | |
completionHandler(data,error); | |
}]; | |
[task resume]; | |
} else { | |
[request setTimeoutInterval:API_RQST_TIME_OUT]; | |
[request setAllowsCellularAccess:YES]; | |
[request setCachePolicy:NSURLRequestReturnCacheDataElseLoad]; | |
[NSURLConnection sendAsynchronousRequest:request queue:sessionDelegate.opQ completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { | |
completionHandler(data,connectionError); | |
}]; | |
} | |
} | |
#pragma mark - NSURLSessionTaskDelegate | |
- (void)URLSession:(NSURLSession *)session | |
task:(NSURLSessionTask *)task | |
didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge | |
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler { | |
// NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengeUseCredential; | |
// NSURLCredential *credential = [NSURLCredential credentialWithUser:CONSUMER_API_USERNAME password:CONSUMER_API_PASSWORD persistence:NSURLCredentialPersistenceNone]; | |
// completionHandler(disposition,credential); | |
} | |
#pragma mark - NSURLSessionDelegate | |
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler { | |
// NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengeUseCredential; | |
// NSURLCredential *credential = [NSURLCredential credentialWithUser:CONSUMER_API_USERNAME password:CONSUMER_API_PASSWORD persistence:NSURLCredentialPersistenceNone]; | |
// completionHandler(disposition,credential); | |
} | |
- (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error { | |
STLog(@"Oh no ! Errors in handling APIs invoked in session - %@",[error description]); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment