Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Created May 7, 2011 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iloveitaly/960618 to your computer and use it in GitHub Desktop.
Save iloveitaly/960618 to your computer and use it in GitHub Desktop.
Modification of AMURLLoader to include a -cancel method
//
// AMURLLoader.h
// FavIconTest
//
// Created by Andreas on 13.09.04.
// Copyright 2004 Andreas Mayer. All rights reserved.
//
// 2004-11-23 Andreas Mayer
// - added delegate
// - init/loaderWithURL will no longer call load automatically
// - removed init/loaderWithURL:...cachePolicy:timeoutInterval:
#import <Cocoa/Cocoa.h>
@interface AMURLLoader : NSObject {
NSMutableData *receivedData;
NSURLRequest *request;
NSURLConnection *connection;
NSMutableDictionary *context;
NSURL *url;
id target;
SEL selector;
id delegate;
}
+ (AMURLLoader *)loaderWithURL:(NSURL *)url target:(id)target selector:(SEL)selector userInfo:(id)theUserInfo;
- (id)initWithURL:(NSURL *)url target:(id)target selector:(SEL)selector userInfo:(id)theUserInfo;
// Does not retain target.
// Expected selector signature:
// - (void)receivedData:(NSData *)data context:(NSDictionary *)context;
// If an error occured, data will be nil.
// The context dictionary may contain:
// key:@"userInfo" value:the user info provided with the init message
// key:@"error" value:NSError object
// key:@"response" value:NSURLResponse object (may be NSHTTPURLResponse - check class)
- (NSURL *)URL;
- (void)setURL:(NSURL *)newURL;
- (id)target;
- (void)setTarget:(id)newTarget;
- (SEL)selector;
- (void)setSelector:(SEL)newSelector;
- (id)delegate;
- (void)setDelegate:(id)newDelegate;
- (void)load;
- (void)cancel;
- (void)loadWithCachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval;
@end
// delegate interface
@interface NSObject (AMURLLoaderDelegate)
- (void)loader:(AMURLLoader *)loader willReceiveDataOfLength:(long long)length;
// tells the delegate how many bytes are to be expected
- (void)loader:(AMURLLoader *)loader didReceiveDataOfLength:(long long)length;
// tells the delegate how many bytes have been received up until now (cumulated)
@end
//
// AMURLLoader.m
// FavIconTest
//
// Created by Andreas on 13.09.04.
// Copyright 2004 Andreas Mayer. All rights reserved.
//
#import "AMURLLoader.h"
@interface AMURLLoader (Private)
- (NSMutableData *)receivedData;
- (void)setReceivedData:(NSMutableData *)newReceivedData;
- (NSURLRequest *)request;
- (void)setRequest:(NSURLRequest *)newRequest;
- (NSURLConnection *)connection;
- (void)setConnection:(NSURLConnection *)newConnection;
- (NSMutableDictionary *)context;
- (void)setContext:(NSMutableDictionary *)newContext;
@end
@implementation AMURLLoader
+ (AMURLLoader *)loaderWithURL:(NSURL *)theURL target:(id)theTarget selector:(SEL)theSelector userInfo:(id)theUserInfo
{
AMURLLoader *result = [[[AMURLLoader alloc] initWithURL:theURL target:theTarget selector:theSelector userInfo:theUserInfo] autorelease];
return result;
}
- (id)initWithURL:(NSURL *)theURL target:(id)theTarget selector:(SEL)theSelector userInfo:(id)theUserInfo
{
// does not retain target
if (self = [super init]) {
[self setReceivedData:[NSMutableData data]];
[self setURL:theURL];
[self setTarget:theTarget];
[self setSelector:theSelector];
[self setContext:[NSMutableDictionary dictionary]];
[[self context] setObject:self forKey:@"AMURLLoader"];
if (theUserInfo) {
[[self context] setObject:theUserInfo forKey:@"userInfo"];
}
[self retain]; // do not release until done
}
return self;
}
- (void)dealloc
{
[receivedData release];
[request release];
[connection release];
[url release];
[super dealloc];
}
- (NSMutableData *)receivedData
{
return receivedData;
}
- (void)setReceivedData:(NSMutableData *)newReceivedData
{
id old = nil;
if (newReceivedData != receivedData) {
old = receivedData;
receivedData = [newReceivedData retain];
[old release];
}
}
- (NSURLRequest *)request
{
return request;
}
- (void)setRequest:(NSURLRequest *)newRequest
{
id old = nil;
if (newRequest != request) {
old = request;
request = [newRequest retain];
[old release];
}
}
- (NSURLConnection *)connection
{
return connection;
}
- (void)setConnection:(NSURLConnection *)newConnection
{
id old = nil;
if (newConnection != connection) {
old = connection;
connection = [newConnection retain];
[old release];
}
}
- (NSMutableDictionary *)context
{
return context;
}
- (void)setContext:(NSMutableDictionary *)newContext
{
id old = nil;
if (newContext != context) {
old = context;
context = [newContext retain];
[old release];
}
}
- (NSURL *)URL
{
return url;
}
- (void)setURL:(NSURL *)newURL
{
id old = nil;
if (newURL != url) {
old = url;
url = [newURL retain];
[old release];
}
}
- (id)target
{
return target;
}
- (void)setTarget:(id)newTarget
{
// do not retain target
target = newTarget;
}
- (SEL)selector
{
return selector;
}
- (void)setSelector:(SEL)newSelector
{
selector = newSelector;
}
- (id)delegate
{
return delegate;
}
- (void)setDelegate:(id)newDelegate
{
// do not retain delegate
delegate = newDelegate;
}
- (void)load
{
[self setRequest:[NSURLRequest requestWithURL:[self URL]]];
if ([self request] != nil) {
[self setConnection:[NSURLConnection connectionWithRequest:[self request] delegate:self]];
}
}
- (void) cancel
{
[connection cancel];
[self setConnection:nil];
[self setRequest:nil];
[context release];
[self release];
}
- (void)loadWithCachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval
{
[self setRequest:[NSURLRequest requestWithURL:[self URL] cachePolicy:cachePolicy timeoutInterval:timeoutInterval]];
if ([self request] != nil) {
[self setConnection:[NSURLConnection connectionWithRequest:[self request] delegate:self]];
}
}
// ============================================================
#pragma mark -
#pragma mark ━ NSURLConnection delegate methods ━
// ============================================================
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[[self context] setObject:response forKey:@"response"];
[[self receivedData] setLength:0];
if ((delegate) && [delegate respondsToSelector:@selector(loader:willReceiveDataOfLength:)]) {
long long length = [response expectedContentLength];
[delegate loader:self willReceiveDataOfLength:length];
}
}
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)theRequest redirectResponse:(NSURLResponse *)redirectResponse
{
return [[theRequest retain] autorelease];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
if (delegate) {
[delegate loader:self didReceiveDataOfLength:[receivedData length]];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[[self context] setObject:error forKey:@"error"];
[[[self context] retain] autorelease];
[target performSelector:selector withObject:nil withObject:[self context]];
[context release];
[self release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[[[self context] retain] autorelease];
[target performSelector:selector withObject:[[[self receivedData] retain] autorelease] withObject:[self context]];
[context release];
[self release];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment