Hi,
I am trying to make a delegate, but can't really get it to work.
This is my code for my fetchURLData.h:
and this is the code for my fetchURLData.m:
The data is retrieved but the log shows this:
The code calling fetchURLData doesn't recieve anything:
The log doesn't show anything
What am I doing wrong?
Best regards,
Paul Peelen
I am trying to make a delegate, but can't really get it to work.
This is my code for my fetchURLData.h:
Code:
#import <Foundation/Foundation.h>
@class fetchURLData;
@protocol fetchURLDataDelegate;
@protocol fetchURLDataDelegate<NSObject>
@optional
- (void)didFinishWithData:(NSData *)fileData;
- (void)didFailWithError:(NSError *)error;
@end
@interface fetchURLData : NSObject {
NSString *theURL;
NSString *theResult;
NSMutableData *receivedData;
long long bytesReceived;
long long expectedBytes;
id<fetchURLDataDelegate> delegate;
}
- (NSString *) getTheURL;
- (NSString *) getTheResult;
- (void) setUrl: (NSString *) getTheURL;
- (void) setResult: (NSString *) getTheResult;
- (void) getData;
@property (assign) id<fetchURLDataDelegate> delegate;
@end
and this is the code for my fetchURLData.m:
Code:
#import "fetchURLData.h"
@implementation fetchURLData
@synthesize delegate;
- (NSString *) getTheURL
{
return theURL;
}
- (NSString *) getTheResult
{
return theResult;
}
- (void) setUrl: (NSString *) getTheURL
{
[theURL autorelease];
theURL = [getTheURL retain];
}
- (void) setResult: (NSString *) getTheResult
{
[theResult autorelease];
theResult = [getTheResult retain];
}
- (void) getData
{
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:theURL]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest
delegate:self];
if (theConnection)
{
receivedData=[[NSMutableData data] retain];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error"
message:@"An error occured while loading your request."
delegate:self
cancelButtonTitle:@"Close"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
expectedBytes = [response expectedContentLength];
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"Bytes: %d of %d", [receivedData length]);
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if (self.delegate != NULL && [self.delegate respondsToSelector:@selector(didFailWithError:)]) {
[self.delegate didFailWithError:error];
}
else {
NSLog(@"Delegate doesn't repond to connector didFailWithError");
}
[connection release];
[receivedData release];
// inform the user
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (self.delegate != NULL && [self.delegate respondsToSelector:@selector(didFinishWithData:)]) {
[self.delegate didFinishWithData:receivedData];
}
else {
NSLog(@"Delegate doesn't repond to connector didFinishWithData");
}
[connection release];
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
NSString *result = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
theResult = result;
[receivedData release];
[connection release];
[result release];
}
- (void) dealloc {
[theURL release];
[theResult release];
[super dealloc];
}
@end
The data is retrieved but the log shows this:
2009-08-23 03:02:39.202 stationen[95324:20b] Bytes: 0 of -1073748872
2009-08-23 03:02:39.204 stationen[95324:20b] Bytes: 1223 of 0
2009-08-23 03:02:39.247 stationen[95324:20b] Bytes: 4079 of 0
2009-08-23 03:02:39.254 stationen[95324:20b] Delegate doesn't repond to connector didFinishWithData
2009-08-23 03:02:39.256 stationen[95324:20b] Succeeded! Received 5815 bytes of data
The code calling fetchURLData doesn't recieve anything:
Code:
- (void)didFinishWithData:(NSData *)fileData
{
NSLog(@"Got some!");
}
- (void)didFailWithError:(NSError *)error
{
NSLog(@"Got error!");
}
What am I doing wrong?
Best regards,
Paul Peelen