Skip to content

Instantly share code, notes, and snippets.

@HeshamMegid
Last active December 16, 2015 02:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save HeshamMegid/5361678 to your computer and use it in GitHub Desktop.
Save HeshamMegid/5361678 to your computer and use it in GitHub Desktop.
An AFHTTPClient subclass that works around the NSURLConnection caching bug in iOS 6.
//
// HMHTTPClient.h
//
// Created by Hesham Abd-Elmegid on 4/10/13.
// Copyright (c) 2013 Startappz. All rights reserved.
//
#import "AFHTTPClient.h"
@interface HMHTTPClient : AFHTTPClient
@end
//
// HMHTTPClient.m
//
// Created by Hesham Abd-Elmegid on 4/10/13.
// Copyright (c) 2013 Startappz. All rights reserved.
//
#import "HMHTTPClient.h"
@implementation HMHTTPClient
- (void)getPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters];
[request setCachePolicy:NSURLRequestReturnCacheDataElseLoad];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
[self enqueueHTTPRequestOperation:operation];
}
- (void)postPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
NSMutableURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters];
[request setCachePolicy:NSURLRequestReturnCacheDataElseLoad];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
[self enqueueHTTPRequestOperation:operation];
}
- (void)putPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
NSMutableURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters];
[request setCachePolicy:NSURLRequestReturnCacheDataElseLoad];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
[self enqueueHTTPRequestOperation:operation];
}
- (void)deletePath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
NSMutableURLRequest *request = [self requestWithMethod:@"DELETE" path:path parameters:parameters];
[request setCachePolicy:NSURLRequestReturnCacheDataElseLoad];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
[self enqueueHTTPRequestOperation:operation];
}
- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
AFHTTPRequestOperation *operation = [super HTTPRequestOperationWithRequest:urlRequest success:success failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (error.code == kCFURLErrorNotConnectedToInternet) {
NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:urlRequest];
if (cachedResponse != nil && [[cachedResponse data] length] > 0) {
id JSON = [NSJSONSerialization JSONObjectWithData:cachedResponse.data options:0 error:&error];
success(operation, JSON);
} else {
failure(operation, error);
}
} else {
failure(operation, error);
}
}];
return operation;
}
@end
@fvisticot
Copy link

When i disable WIFI on my iPad, the errorCode is kCFURLErrorCannotConnectToHost and not kCFURLErrorNotConnectedToInternet...
Do we need to manage other errorCode for a more robust solution ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment