Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Mac > Mac Programmer Help > Re: (Obj-C) One...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 6 Topic 3733 of 3882
Post > Topic >>

Re: (Obj-C) One of these things is not like the other...

by David Phillip Oster <oster@[EMAIL PROTECTED] > May 6, 2008 at 08:33 PM

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]

 




 6 Posts in Topic:
(Obj-C) One of these things is not like the other...
Don Bruder <dakidd@[EM  2008-05-06 14:04:10 
Re: (Obj-C) One of these things is not like the other...
Gregory Weston <uce@[E  2008-05-06 18:52:29 
Re: (Obj-C) One of these things is not like the other...
Don Bruder <dakidd@[EM  2008-05-06 18:57:46 
Re: (Obj-C) One of these things is not like the other...
David Phillip Oster <o  2008-05-06 20:27:56 
Re: (Obj-C) One of these things is not like the other...
David Phillip Oster <o  2008-05-06 20:33:31 
Re: (Obj-C) One of these things is not like the other...
David Phillip Oster <o  2008-05-06 23:38:16 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Dec 5 10:56:36 CST 2008.