实现NSURLConnectionDataDelegate协议

3

我是iOS开发的新手。我正在尝试实现NSURLConnectionDataDelegate协议,但似乎没有任何代理方法被调用。我不得不亲自键入代理方法,这是否应该自动生成?

每个代理方法中,我都有一个NSLog命令,但什么也没有输出。我正在使用NSURLConnection异步下载并跟踪进度,以便稍后更新progressView。

SearchFeed.h文件(请注意,我在键入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文件:

#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);
}

我已经将NSURLConnection的代理设置为SearchFeed.m类中的self。在SearchFeed.h中,我尝试实现NSURLConnectionDataDelegate协议。我必须创建connectionDidFinishLoading、didReceiveResponse和didReceiveData方法,但这些方法没有被调用。
我可能没有正确地实现协议,或者我已经将一些方法声明为+和-(一些方法是类方法,而一些方法是实例方法)。
downloadPDFToMyDocumentsFrom是一个类方法,当用户点击下载时被调用。此方法设置NSURLConnection,设置URL等,并打开文件流以接收数据。然而,其他方法都没有被调用。

https://dev59.com/O2HVa4cB1Zd3GeqPkTTo#9577548 - Eric
1个回答

2

你的downloadPDFToMyDocumentsFrom方法被设置为类方法(+),而你将代理设置为self,即表示在这种情况下的类。你应该将downloadPDFToMyDocumentsFrom方法设置为实例方法(-),这样self就是一个实例化的对象。


有道理。在这种情况下,self指向类名,而将其更改为实例方法则指向类的实例。我感觉是这样,但我不愿意改变一切。 - Bilal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接