Skip to content

Instantly share code, notes, and snippets.

@sag333ar
Last active December 28, 2015 09:59
Show Gist options
  • Save sag333ar/7482594 to your computer and use it in GitHub Desktop.
Save sag333ar/7482594 to your computer and use it in GitHub Desktop.
NSURLSession, NSURLSessionConfiguration, NSURLSessionDelegate, NSURLSessionDataTask, AFNetworking, AFHTTPRequestOperation, Core-Class
#define IS_IOS_7 ([[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."][0] intValue] >= 7)
#define API_RQST_TIME_OUT 30
// part of header file --------------------------------------------------------
#import <Foundation/Foundation.h>
@protocol API_Handler <NSObject>
@required
- (void)didSuccess:(NSData*)data;
- (void)didFail:(NSError*)error;
@end
@interface SessionDelegate : NSObject <NSURLSessionDataDelegate, NSURLSessionDelegate, NSURLSessionTaskDelegate>
+ (id)getDataWithRequest:(NSMutableURLRequest*)request andHandler:(id <API_Handler>)handler;
@end
@interface SessionConfiguration : NSObject
@end
// ----------------------------------------------------------------------------------------------------------------
// part of implementation file --------------------------------------------------------
#import "SessionDelegate.h"
static NSURLSessionConfiguration *sharedSessionConfig;
@interface SessionConfiguration ()
+ (NSURLSessionConfiguration*)sessionConfig;
@end
@implementation SessionConfiguration
+ (void)initialize {
sharedSessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
sharedSessionConfig.timeoutIntervalForRequest = API_RQST_TIME_OUT;
sharedSessionConfig.allowsCellularAccess = YES;
sharedSessionConfig.requestCachePolicy = NSURLRequestReloadIgnoringCacheData;
}
+ (NSURLSessionConfiguration*)sessionConfig {
return sharedSessionConfig;
}
@end
@interface SessionConfiguration ()
+ (NSURLSessionConfiguration*)sessionConfig;
@end
@interface SessionDelegate ()
+ (SessionDelegate*)sharedSessionDelegate;
@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 - Requests
+ (id)getDataWithRequest:(NSMutableURLRequest*)request andHandler:(id <API_Handler>)handler {
__block id handler_for_block = handler;
if(IS_IOS_7) {
NSURLSessionDataTask *task = [sharedSession dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(!error) {
[handler_for_block didSuccess:data];
} else {
[handler_for_block didFail:error];
}
}];
[task resume];
return task;
} else {
[request setTimeoutInterval:API_RQST_TIME_OUT];
[request setAllowsCellularAccess:YES];
[request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
NSURLCredential *cred = [NSURLCredential credentialWithUser:<#username#> password:<#password#> persistence:NSURLCredentialPersistenceForSession];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[handler_for_block didSuccess:[operation responseData]];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[handler_for_block didFail:error];
}];
operation.credential = cred;
[operation start];
return operation;
}
}
- (void)URLSession:(NSURLSession *)session
task:(NSURLSessionTask *)task
didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengeUseCredential;
NSURLCredential *credential = [NSURLCredential credentialWithUser:<#username#> password:<#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:<#username#> password:<#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