Last active
December 16, 2015 02:19
-
-
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.
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
// | |
// 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 |
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
// | |
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 ?