Skip to content

Instantly share code, notes, and snippets.

@leovandriel
Created September 27, 2012 15:59
Show Gist options
  • Save leovandriel/3794804 to your computer and use it in GitHub Desktop.
Save leovandriel/3794804 to your computer and use it in GitHub Desktop.
NWURLConnection - an NSURLConnectionDelegate based on blocks with cancel
// NWURLConnection - an NSURLConnectionDelegate based on blocks with cancel.
// Similar to the `sendAsynchronousRequest:` method of NSURLConnection, but
// with `cancel` method. Requires ARC on iOS 6 or Mac OS X 10.8.
// License: BSD
// Author: Leonard van Driel, 2012
@interface NWURLConnection : NSObject<NSURLConnectionDelegate>
@property (nonatomic, strong) NSURLRequest *request;
@property (nonatomic, strong) NSOperationQueue *queue;
@property (nonatomic, 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 {
NSURLConnection *connection;
NSHTTPURLResponse *response;
NSMutableData *responseData;
}
@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];
}
- (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
@bonoboslabs
Copy link

Thanks, works like a charm.

danielkramer: all you need to do is call cancel on returned instance of NWURLConnection.

import "NWURLConnection.h"

NWURLConnection *connection = [NWURLConnection sendAsynchronousRequest.......];
[connection cancel];

hope it helped.

@carbamide
Copy link

Beautiful. Thank you so much for this.

@charlescarver
Copy link

I love this.

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