Skip to content

Instantly share code, notes, and snippets.

@insanj
Last active December 17, 2015 19:59
Show Gist options
  • Save insanj/5664857 to your computer and use it in GitHub Desktop.
Save insanj/5664857 to your computer and use it in GitHub Desktop.
NWURLConnection without ARC. Original: https://gist.github.com/leonardvandriel/3794804
// NWURLConnection - an NSURLConnectionDelegate based on blocks with cancel.
// Similar to the `sendAsynchronousRequest:` method of NSURLConnection, but
// with `cancel` method. Does not require ARC on iOS 6 or Mac OS X 10.8.
// License: BSD
// Author: Leonard van Driel, 2012
// Converted to non-ARC by Julian "insanj" Weiss, 2013
@interface NWURLConnection : NSObject <NSURLConnectionDelegate>{
NSURLConnection *connection;
NSHTTPURLResponse *response;
NSMutableData *responseData;
}
@property (retain) NSURLRequest *request;
@property (retain) NSOperationQueue *queue;
@property (copy) void(^completionHandler)(NSURLResponse *response, NSData *data, NSError *error);
+ (NWURLConnection *)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void(^)(NSURLResponse *response, NSData *data, NSError *error))completionHandler;
- (void)start;
- (void)cancel;
@end
@implementation NWURLConnection
@synthesize request, queue, completionHandler;
+ (NWURLConnection *)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void(^)(NSURLResponse *response, NSData *data, NSError *error))completionHandler
{
NWURLConnection *result = [[NWURLConnection alloc] init];
result.request = request;
result.queue = queue;
result.completionHandler = completionHandler;
[result start];
return result;
}
- (void)dealloc
{
[self cancel];
NSLog(@"in NW dealloc");
NSLog(@"connection");
[connection release];
NSLog(@"response");
[response release];
NSLog(@"responsedata");
[responseData release];
NSLog(@"super");
[super dealloc];
}
- (void)start
{
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[connection scheduleInRunLoop:NSRunLoop.mainRunLoop forMode:NSDefaultRunLoopMode];
if (connection) {
[connection start];
} else {
if (completionHandler) completionHandler(nil, nil, nil); completionHandler = nil;
}
}
- (void)cancel
{
[connection cancel]; connection = nil;
completionHandler = nil;
}
- (void)connection:(NSURLConnection *)_connection didReceiveResponse:(NSHTTPURLResponse *)_response
{
response = _response;
}
- (void)connection:(NSURLConnection *)_connection didReceiveData:(NSData *)data
{
if (!responseData) {
responseData = [NSMutableData dataWithData:data];
} else {
[responseData appendData:data];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)_connection
{
connection = nil;
if (completionHandler) {
void(^b)(NSURLResponse *response, NSData *data, NSError *error) = completionHandler;
completionHandler = nil;
[queue addOperationWithBlock:^{b(response, responseData, nil);}];
}
}
- (void)connection:(NSURLConnection *)_connection didFailWithError:(NSError *)error
{
connection = nil;
if (completionHandler) {
void(^b)(NSURLResponse *response, NSData *data, NSError *error) = completionHandler;
completionHandler = nil;
[queue addOperationWithBlock:^{b(response, responseData, error);}];
}
}
#if TARGET_IPHONE_SIMULATOR
- (BOOL)connection:(NSURLConnection *)_connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)_connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}
#endif
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment