In article <Ivmdnf5LbqkGkbzVnZ2dnUVZ_ournZ2d@[EMAIL PROTECTED]
>,
Don Bruder <dakidd@[EMAIL PROTECTED]
> wrote:
> is there a simple, non-sync way of doing the deed? I envision a fantasy
> to the tune of "Here's a URL and a callback. Holler via the callback
> when you've got the needed data so we can proceed"
/* AppDelegate.h - tested, works */
#im****t <Cocoa/Cocoa.h>
@[EMAIL PROTECTED]
AppDelegate : NSObject {
NSData *data_;
NSURLConnection *connection_;
}
- (void)setConnection:(NSURLConnection *)connection;
- (void)setData:(NSData *)data;
@[EMAIL PROTECTED]
AppDelegate.m */
#im****t "AppDelegate.h"
@[EMAIL PROTECTED]
AppDelegate
- (void)dealloc {
[data_ release];
[connection_ release];
[super dealloc];
}
-(void)awakeFromNib{
NSURL *url = [NSURL URLWithString:@[EMAIL PROTECTED]
"http://www.apple.com/"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [NSURLConnection
connectionWithRequest:request delegate:self];
[self setConnection:connection];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData
*)data {
if (nil != data_) {
NSMutableData *data2 = [[data_ mutableCopy] autorelease];
[data2 appendData:data];
data = data2;
}
[self setData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if (data_) {
NSLog(@[EMAIL PROTECTED]
"%@[EMAIL PROTECTED]
", [[[NSString alloc] initWithData:data_
encoding:NSUTF8StringEncoding] autorelease]);
[self setData:nil];
}
[self setConnection:nil];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError
*)error {
NSLog(@[EMAIL PROTECTED]
"%@[EMAIL PROTECTED]
", error);
[self setData:nil];
[self setConnection:nil];
}
- (void)setConnection:(NSURLConnection *)connection {
[connection_ autorelease];
connection_ = [connection retain];
}
- (void)setData:(NSData *)data {
[data_ autorelease];
data_ = [data retain];
}
@[EMAIL PROTECTED]


|