我是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 urlConnection的委托设置为self,即SearchFeed.m类。 在SearchFeed.h中,我尝试实现NSURLConnectionDataDelegate协议。 我不得不创建connectionDidFinishLoading,didReceiveResponse和didReceiveData方法,但这些方法不会被调用。
我要么没有正确实现协议,要么我已经将某些方法声明为+,而将某些方法声明为 - (某些方法是类方法,而有些方法是实例方法)
downloadPDFToMyDocumentsFrom是一种在用户点击下载时调用的类方法。 此方法设置NSURLConnection,设置URL等和委托,并打开fileStream以接收数据。但是,没有其他方法被调用。
答案 0 :(得分:2)
您的downloadPDFToMyDocumentsFrom
方法设置为类方法(+
),并将您的委托设置为self
,这意味着此类中的类。您应该将downloadPDFToMyDocumentsFrom
方法设为实例方法(-
),以便self是实例化对象。