Created
September 27, 2012 15:59
-
-
Save leovandriel/3794804 to your computer and use it in GitHub Desktop.
NWURLConnection - an NSURLConnectionDelegate based on blocks with cancel
This file contains 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
// 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 |
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.
Beautiful. Thank you so much for this.
I love this.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you provide an example using it with a cancel?