Start work on moving delegate stuff to protocol
Add request:didReceiveData: delegate method
Showing
5 changed files
with
75 additions
and
9 deletions
@@ -16,6 +16,8 @@ | @@ -16,6 +16,8 @@ | ||
16 | #endif | 16 | #endif |
17 | #import <stdio.h> | 17 | #import <stdio.h> |
18 | #import "ASIHTTPRequestConfig.h" | 18 | #import "ASIHTTPRequestConfig.h" |
19 | +#import "ASIHTTPRequestDelegate.h" | ||
20 | +#import "ASIProgressDelegate.h" | ||
19 | 21 | ||
20 | extern NSString *ASIHTTPRequestVersion; | 22 | extern NSString *ASIHTTPRequestVersion; |
21 | 23 | ||
@@ -62,10 +64,11 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount; | @@ -62,10 +64,11 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount; | ||
62 | NSURL *originalURL; | 64 | NSURL *originalURL; |
63 | 65 | ||
64 | // The delegate, you need to manage setting and talking to your delegate in your subclasses | 66 | // The delegate, you need to manage setting and talking to your delegate in your subclasses |
65 | - id delegate; | 67 | + id <ASIHTTPRequestDelegate> delegate; |
66 | 68 | ||
67 | - // A queue delegate that should *ALSO* be notified of delegate message (used by ASINetworkQueue) | 69 | + // Another delegate that is also notified of request status changes and progress updates |
68 | - id queue; | 70 | + // Generally, you won't use this directly, but ASINetworkQueue sets itself as the queue so it can proxy updates to its own delegates |
71 | + id <ASIHTTPRequestDelegate, ASIProgressDelegate> queue; | ||
69 | 72 | ||
70 | // HTTP method to use (GET / POST / PUT / DELETE / HEAD). Defaults to GET | 73 | // HTTP method to use (GET / POST / PUT / DELETE / HEAD). Defaults to GET |
71 | NSString *requestMethod; | 74 | NSString *requestMethod; |
@@ -159,10 +162,10 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount; | @@ -159,10 +162,10 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount; | ||
159 | NSString *proxyDomain; | 162 | NSString *proxyDomain; |
160 | 163 | ||
161 | // Delegate for displaying upload progress (usually an NSProgressIndicator, but you can supply a different object and handle this yourself) | 164 | // Delegate for displaying upload progress (usually an NSProgressIndicator, but you can supply a different object and handle this yourself) |
162 | - id uploadProgressDelegate; | 165 | + id <ASIProgressDelegate> uploadProgressDelegate; |
163 | 166 | ||
164 | // Delegate for displaying download progress (usually an NSProgressIndicator, but you can supply a different object and handle this yourself) | 167 | // Delegate for displaying download progress (usually an NSProgressIndicator, but you can supply a different object and handle this yourself) |
165 | - id downloadProgressDelegate; | 168 | + id <ASIProgressDelegate> downloadProgressDelegate; |
166 | 169 | ||
167 | // Whether we've seen the headers of the response yet | 170 | // Whether we've seen the headers of the response yet |
168 | BOOL haveExaminedHeaders; | 171 | BOOL haveExaminedHeaders; |
@@ -23,7 +23,7 @@ | @@ -23,7 +23,7 @@ | ||
23 | 23 | ||
24 | 24 | ||
25 | // Automatically set on build | 25 | // Automatically set on build |
26 | -NSString *ASIHTTPRequestVersion = @"v1.6.1-10 2010-04-13"; | 26 | +NSString *ASIHTTPRequestVersion = @"v1.6.1-11 2010-04-13"; |
27 | 27 | ||
28 | NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain"; | 28 | NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain"; |
29 | 29 | ||
@@ -2451,8 +2451,20 @@ static BOOL isiPhoneOS2; | @@ -2451,8 +2451,20 @@ static BOOL isiPhoneOS2; | ||
2451 | return; | 2451 | return; |
2452 | } | 2452 | } |
2453 | 2453 | ||
2454 | + // Does the delegate want to handle the data manually? | ||
2455 | + SEL receivedDataSelector = @selector(request:didReceiveData:); | ||
2456 | + if ([[self delegate] respondsToSelector:receivedDataSelector]) { | ||
2457 | + NSMethodSignature *signature = [[[self delegate] class] instanceMethodSignatureForSelector:receivedDataSelector]; | ||
2458 | + NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; | ||
2459 | + [invocation setTarget:[self delegate]]; | ||
2460 | + [invocation setSelector:receivedDataSelector]; | ||
2461 | + [invocation setArgument:self atIndex:2]; | ||
2462 | + [invocation setArgument:[NSData dataWithBytes:buffer length:bytesRead] atIndex:3]; | ||
2463 | + [invocation performSelectorOnMainThread:@selector(invokeWithTarget:) withObject:[self delegate] waitUntilDone:[NSThread isMainThread]]; | ||
2464 | + | ||
2465 | + | ||
2454 | // Are we downloading to a file? | 2466 | // Are we downloading to a file? |
2455 | - if ([self downloadDestinationPath]) { | 2467 | + } else if ([self downloadDestinationPath]) { |
2456 | if (![self fileDownloadOutputStream]) { | 2468 | if (![self fileDownloadOutputStream]) { |
2457 | BOOL append = NO; | 2469 | BOOL append = NO; |
2458 | if (![self temporaryFileDownloadPath]) { | 2470 | if (![self temporaryFileDownloadPath]) { |
Classes/ASIHTTPRequestDelegate.h
0 → 100644
1 | +// | ||
2 | +// ASIHTTPRequestDelegate.h | ||
3 | +// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest | ||
4 | +// | ||
5 | +// Created by Ben Copsey on 13/04/2010. | ||
6 | +// Copyright 2010 All-Seeing Interactive. All rights reserved. | ||
7 | +// | ||
8 | + | ||
9 | +#import <Cocoa/Cocoa.h> | ||
10 | +@class ASIHTTPRequest; | ||
11 | + | ||
12 | +@protocol ASIHTTPRequestDelegate <NSObject> | ||
13 | + | ||
14 | +@optional | ||
15 | + | ||
16 | +// These are the default delegate methods for request status | ||
17 | +// You can use different ones by setting didStartSelector / didFinishSelector / didFailSelector | ||
18 | +- (void)requestStarted:(ASIHTTPRequest *)request; | ||
19 | +- (void)requestFinished:(ASIHTTPRequest *)request; | ||
20 | +- (void)requestFailed:(ASIHTTPRequest *)request; | ||
21 | + | ||
22 | +// When a delegate implements this method, it is expected to process all incoming data itself | ||
23 | +// This means that responseData / responseString / downloadDestinationPath etc are ignored | ||
24 | +- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data; | ||
25 | + | ||
26 | +// If a delegate implements one of these, it will be asked to supply credentials when none are available | ||
27 | +// The delegate can then either restart the request ([request retryUsingSuppliedCredentials]) once credentials have been set | ||
28 | +// or cancel it ([request cancelAuthentication]) | ||
29 | +- (void)authenticationNeededForRequest:(ASIHTTPRequest *)request; | ||
30 | +- (void)proxyAuthenticationNeededForRequest:(ASIHTTPRequest *)request; | ||
31 | + | ||
32 | +@end |
1 | // | 1 | // |
2 | // ASIProgressDelegate.h | 2 | // ASIProgressDelegate.h |
3 | -// Mac | 3 | +// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest |
4 | // | 4 | // |
5 | // Created by Ben Copsey on 13/04/2010. | 5 | // Created by Ben Copsey on 13/04/2010. |
6 | // Copyright 2010 All-Seeing Interactive. All rights reserved. | 6 | // Copyright 2010 All-Seeing Interactive. All rights reserved. |
@@ -9,11 +9,30 @@ | @@ -9,11 +9,30 @@ | ||
9 | #import <Cocoa/Cocoa.h> | 9 | #import <Cocoa/Cocoa.h> |
10 | @class ASIHTTPRequest; | 10 | @class ASIHTTPRequest; |
11 | 11 | ||
12 | -@protocol ASIProgressDelegate | 12 | +@protocol ASIProgressDelegate <NSObject> |
13 | 13 | ||
14 | @optional | 14 | @optional |
15 | + | ||
16 | +// These methods are used to update UIProgressViews (iPhone OS) or NSProgressIndicators (Mac OS X) | ||
17 | +// If you are using a custom progress delegate, you may find it easier to implement didReceiveBytes / didSendBytes instead | ||
18 | +#if TARGET_OS_IPHONE | ||
19 | +- (void)setProgress:(float)newProgress; | ||
20 | +#else | ||
21 | +- (void)setDoubleValue:(double)newProgress; | ||
22 | +- (void)setMaxValue:(double)newMax; | ||
23 | +#endif | ||
24 | + | ||
25 | +// Called when the request recieves some data - bytes is the length of that data | ||
26 | +// bytes may be less than zero if a request needs to remove its progress so far | ||
15 | - (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes; | 27 | - (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes; |
28 | + | ||
29 | +// Called when the request sends some data. | ||
30 | +// The first 32KB (128KB on older platforms) of data sent is not included in this amount because of limitations with the CFNetwork API | ||
16 | - (void)request:(ASIHTTPRequest *)request didSendBytes:(long long)bytes; | 31 | - (void)request:(ASIHTTPRequest *)request didSendBytes:(long long)bytes; |
32 | + | ||
33 | +// Called when a request needs to change the length of the content to download | ||
17 | - (void)request:(ASIHTTPRequest *)request resetDownloadContentLength:(long long)newLength; | 34 | - (void)request:(ASIHTTPRequest *)request resetDownloadContentLength:(long long)newLength; |
35 | + | ||
36 | +// Called when a request needs to change the length of the content to upload | ||
18 | - (void)request:(ASIHTTPRequest *)request resetUploadContentLength:(long long)newLength; | 37 | - (void)request:(ASIHTTPRequest *)request resetUploadContentLength:(long long)newLength; |
19 | @end | 38 | @end |
This diff was suppressed by a .gitattributes entry.
-
Please register or login to post a comment