Declared in NSURLConnection+BlocksKit.h

Overview

NSURLConnection with both delegate and block callback support.

It also adds useful block handlers for tracking upload and download progress.

Here is a small example: - (void)downloadImage:(id)sender { self.downloadButton.enabled = NO; self.progressView.progress = 0.0f; NSURL imageURL = [NSURL URLWithString:@“http://icanhascheezburger.files.wordpress.com/2011/06/funny-pictures-nyan-cat-wannabe1.jpg”]; NSURLRequest request = [NSURLRequest requestWithURL:imageURL]; NSURLConnection connection = [NSURLConnection connectionWithRequest:request]; connection.delegate = self; connection.failureBlock = ^(NSURLConnection connection, NSError *error) { [[UIAlertView alertViewWithTitle:@“Download error” message:[error localizedDescription]] show];

         self.downloadButton.enabled = YES;
         self.progressView.progress = 0.0f;
     };
     connection.successBlock = ^(NSURLConnection *connection, NSURLResponse *response, NSData *responseData) {
         self.imageView.image = [UIImage imageWithData:responseData];
         self.downloadButton.enabled = YES;
     };
     connection.downloadBlock = ^(double progress) {
         self.progressView.progress = progress;
     };

     [connection start];
 }

 //these methods will be called too!
 - (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
     NSLog(@"%s",__PRETTY_FUNCTION__);
 }

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
     NSLog(@"%s",__PRETTY_FUNCTION__);
 }

Created by Igor Evsukov as IEURLConnection and contributed to BlocksKit.

Tasks

Properties

bk_downloadBlock

@property (nonatomic, copy, setter=bk_setDownloadBlock:) void ( ^ ) ( double percent ) bk_downloadBlock
Discussion

The block fired every time new data is recieved from the server, representing the current percentage of completion.

This block corresponds to the connection:didRecieveData: method of NSURLConnectionDelegate.

Declared In

NSURLConnection+BlocksKit.h

bk_failureBlock

@property (nonatomic, copy, setter=bk_setFailureBlock:) void ( ^ ) ( NSURLConnection *connection , NSError *error ) bk_failureBlock
Discussion

The block fired upon the failure of the connection.

This block corresponds to the connection:didFailWithError: method of NSURLConnectionDelegate.

Declared In

NSURLConnection+BlocksKit.h

bk_responseBlock

@property (nonatomic, copy, setter=bk_setResponseBlock:) void ( ^ ) ( NSURLConnection *connection , NSURLResponse *response ) bk_responseBlock
Discussion

The block fired once the connection recieves a response from the server.

This block corresponds to the connection:didReceiveResponse: method of NSURLConnectionDataDelegate.

Declared In

NSURLConnection+BlocksKit.h

bk_successBlock

@property (nonatomic, copy, setter=bk_setSuccessBlock:) void ( ^ ) ( NSURLConnection *connection , NSURLResponse *response , NSData *responseData ) bk_successBlock
Discussion

The block that upon the successful completion of the connection.

This block corresponds to the connectionDidFinishLoading: method of NSURLConnectionDelegate.

Warning: If the delegate implements connection:didRecieveData:, then this block will not include the data recieved by the connection and appending the recieved data to an instance NSMutableData is left up to the user due to the behavior of frameworks that use NSURLConnection.

Declared In

NSURLConnection+BlocksKit.h

bk_uploadBlock

@property (nonatomic, copy, setter=bk_setUploadBlock:) void ( ^ ) ( double percent ) bk_uploadBlock
Discussion

The block fired every time new data is sent to the server, representing the current percentage of completion.

This block corresponds to the connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: method of NSURLConnectionDelegate.

Declared In

NSURLConnection+BlocksKit.h

delegate

@property (nonatomic, weak, setter=setDelegate:) id delegate
Discussion

A mutable delegate that implements the NSURLConnectionDelegate protocol.

This property allows for both use of block callbacks and delegate methods in an instance of NSURLConnection. It only works on block-backed NSURLConnection instances.

Declared In

NSURLConnection+BlocksKit.h

Class Methods

bk_connectionWithRequest:

+ (NSURLConnection *)bk_connectionWithRequest:(NSURLRequest *)request
Discussion

Creates and returns an initialized block-backed URL connection that does not begin to load the data for the URL request.

Parameters

request

The URL request to load.

Return Value

An autoreleased NSURLConnection for the specified URL request.

Declared In

NSURLConnection+BlocksKit.h

bk_startConnectionWithRequest:successHandler:failureHandler:

+ (NSURLConnection *)bk_startConnectionWithRequest:(NSURLRequest *)request successHandler:(void ( ^ ) ( NSURLConnection *connection , NSURLResponse *response , NSData *responseData ))success failureHandler:(void ( ^ ) ( NSURLConnection *connection , NSError *error ))failure
Discussion

Creates, starts, and returns an asynchronous, block-backed URL connection

Parameters

request

The URL request to load.

success

A code block that acts on instances of NSURLResponse and NSData in the event of a successful connection.

failure

A code block that acts on instances of NSURLResponse and NSError in the event of a failed connection.

Return Value

New and running NSURLConnection with the specified properties.

Declared In

NSURLConnection+BlocksKit.h

Instance Methods

bk_initWithRequest:

- (id)bk_initWithRequest:(NSURLRequest *)request
Discussion

Returns an initialized block-backed URL connection.

Parameters

request

The URL request to load.

Return Value

Newly initialized NSURLConnection with the specified properties.

Declared In

NSURLConnection+BlocksKit.h

bk_initWithRequest:completionHandler:

- (id)bk_initWithRequest:(NSURLRequest *)request completionHandler:(void ( ^ ) ( NSURLConnection *connection , NSURLResponse *response , NSData *responseData ))block
Discussion

Returns an initialized URL connection with the specified completion handler.

Parameters

request

The URL request to load.

block

A code block that acts on instances of NSURLResponse and NSData in the event of a successful connection.

Return Value

Newly initialized NSURLConnection with the specified properties.

Declared In

NSURLConnection+BlocksKit.h

bk_startWithCompletionBlock:

- (void)bk_startWithCompletionBlock:(void ( ^ ) ( NSURLConnection *connection , NSURLResponse *response , NSData *responseData ))block
Discussion

Causes the connection to begin loading data, if it has not already, with the specified block to be fired on successful completion.

Parameters

block

A code block that acts on instances of NSURLResponse and NSData in the event of a successful connection.

Declared In

NSURLConnection+BlocksKit.h