Protocol not called with NSURLConnection

Discussion in 'iOS Development' started by NolesFans, Jul 17, 2009.

  1. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    Header
    [OBJC]#import <Foundation/Foundation.h>

    @protocol AsyncNetworkDelegate;

    @interface AsyncNetwork : NSObject {
    NSURLConnection *downloadConnection;
    NSMutableData *receivedData;
    id<AsyncNetworkDelegate> delegate;
    BOOL done;
    }

    @property (nonatomic, assign) id<AsyncNetworkDelegate> delegate;
    @property (nonatomic, retain) NSMutableData *receivedData;
    @property (nonatomic, readonly, retain) NSURLConnection *downloadConnection;

    - (id)initWithString

    Please Register or Log in to view images

    NSString*)urlString;
    @end

    @protocol AsyncNetworkDelegate<NSObject>
    - (void)network

    Please Register or Log in to view images

    AsyncNetwork*)network didFailWithError

    Please Register or Log in to view images

    NSError*)error;
    - (void)network

    Please Register or Log in to view images

    AsyncNetwork*)network didFinishWithData

    Please Register or Log in to view images

    NSData*)data;
    @end
    [/OBJC]

    [OBJC]
    #import "AsyncNetwork.h"

    @implementation AsyncNetwork

    @synthesize delegate, receivedData, downloadConnection;

    - (id)initWithString

    Please Register or Log in to view images

    NSString*)urlString
    {
    if (self = [super init]) {
    done = NO;
    self.receivedData = [NSMutableData data];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
    downloadConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (downloadConnection != nil) {
    do {
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    } while (!done);
    }
    [downloadConnection start];
    }
    return self;
    }

    #pragma mark NSURLConnection delegate methods

    // The following are delegate methods for NSURLConnection. Similar to callback functions, this is how the connection object,
    // which is working in the background, can asynchronously communicate back to its delegate on the thread

    - (void)connection

    Please Register or Log in to view images

    NSURLConnection*)connection didReceiveData

    Please Register or Log in to view images

    NSData*)data
    {
    [receivedData appendData:data];
    }

    - (void)connection

    Please Register or Log in to view images

    NSURLConnection*)connection didFailWithError

    Please Register or Log in to view images

    NSError*)error
    {
    done = YES;
    [self.delegate network:self didFailWithError:error];
    }

    - (void)connectionDidFinishLoading

    Please Register or Log in to view images

    NSURLConnection*)connection
    {
    [self.delegate network:self didFinishWithData:receivedData]; << not call
    // Set the condition which ends the run loop.
    done = YES;
    }

    - (void)dealloc {
    [receivedData release];
    [downloadConnection release];
    [super dealloc];
    }

    @end
    [/OBJC]

    There is no warning.
    ------------------double post merged------------------
    never mind, I found it.
  2. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    You should either add an '@required' to your protocol method list, or check if your delegate has implemented that method.

    Ex:

    [OBJC]
    @procotol blah<NSObject>
    @required
    - (NSString *)importantName;
    @end
    [/OBJC]

    Or:

    [OBJC]
    if([delegate respondsToSelector

    Please Register or Log in to view images

    selector(importantName)]) {
    self.importantName = [delegate importantName];
    }
    [/OBJC]

Share This Page