Implementing NSURLConnectionDataDelegate Protocol

2.1k views Asked by At

I am new to iOS development. I am trying to implement NSURLConnectionDataDelegate Protocol but it seems that none of the delegate methods ever get called. I had to type the delegate methods in myself, is it supposed to be automatically generated?

I have an NSLog command in each delegate method but nothing prints. I am using NSURLConnection to Asynchronously download and keep track of the progress so I can update a progressView later.

SearchFeed.h file (Notice I have tried to implement the protocol when I typed NSURLConnectionDataDelegate

#import <Foundation/Foundation.h>
#import "Doc.h"


@interface SearchFeed : NSObject <NSXMLParserDelegate, NSURLConnectionDataDelegate>
{
    NSMutableString * currentElementValue;

    Doc *currentDoc;


}
@property(strong,nonatomic) NSURL * searchUrl;
@property(strong,nonatomic) NSArray * searchResults;
//@property(retain, nonatomic) Doc * currentDoc;
@property(retain, nonatomic) NSMutableArray *docs;
//@property(retain, nonatomic) NSURLConnection *urlConnection;
@property(retain, nonatomic) UIProgressView * progressBar;


-(void)retrieveFromInternet;
-(double) getProgress;

+(NSString *)pathToDocuments;
+(void)downloadPDFToMyDocumentsFrom:(NSString*) PDFUrl filename:(NSString *) title;
+(NSArray *)listFilesAtPath:(NSString *)path;
@end

SearchFeed.m file:

#import "SearchFeed.h"

@implementation SearchFeed

@synthesize searchUrl = _searchUrl; //where to search from
@synthesize searchResults = _searchResults; // Not being used -- I think
//@synthesize currentDoc = _currentDoc; //current Doc
@synthesize docs = _docs; //array of Docs
@synthesize progressBar = _progressBar; 



NSURLConnection *urlConnection;
double fileLength =0;
double lastProgress =0;
double currentLength =0;
NSOutputStream *fileStream;

+(void)downloadPDFToMyDocumentsFrom:(NSString*) PDFUrl filename:(NSString *) title {

NSURL *url = [[NSURL alloc] initWithString:PDFUrl];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];


NSString *fileName = [title stringByAppendingPathExtension:@"pdf"];
NSString *filePath = [[self pathToDocuments] stringByAppendingPathComponent:fileName];

fileStream = [[NSOutputStream alloc] initToFileAtPath:filePath append:YES];

[fileStream open];
}
//handling incoming data
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    double length = [data length];
    currentLength += length;
    double progress = currentLength/fileLength;

    NSLog(@"Receiving data");

    if(lastProgress < progress)
    {
        //progressBar WRITE code to update the progress for the progress bar

        lastProgress = progress;
        self.progressBar.progress = lastProgress;

        NSLog(@"%f -------------------------------------------------------", lastProgress);
    }

    NSUInteger left = [data length];
    NSUInteger nwr = 0;

    do {
        nwr = [fileStream write:[data bytes] maxLength:left];

        if(nwr == -1)
            break;
        left -= nwr;
    }while(left>0);

    if(left)
    {
        NSLog(@"Stream error: %@", [fileStream streamError]);
    }
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    long length = [response expectedContentLength];
    fileLength = length;

     NSLog(@"%f ------------------------------------------------------- is the fileLength", fileLength);
}

//handling connection progress

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
        //WRITE code to set the progress bar to 1.0
    self.progressBar.progress = 1.0;
    [fileStream close];
     NSLog(@"%f -------------------------------------------------------", lastProgress);
}

I have set the delegate for NSURLConnection urlConnection to self which is SearchFeed.m class. In SearchFeed.h, I tried to implement the NSURLConnectionDataDelegate protocol. I had to create connectionDidFinishLoading, didReceiveResponse and didReceiveData methods but those methods don't get called.

I either have not implemented the protocol properly OR I have declared some methods as + and some as - (some methods are class methods while some are instance methods)

downloadPDFToMyDocumentsFrom is a class method which is invoked when the user clicks download. This method sets the NSURLConnection, sets the URL etc and the delegate and opens the fileStream to receive data. However, none of the other methods get called.

1

There are 1 answers

1
coneybeare On

Your downloadPDFToMyDocumentsFrom method is setup as a class method (+), and you setup your delegate to be self, meaning the Class in this case. You should make the downloadPDFToMyDocumentsFrom method a instance method (-) so that self is an instantiated object.