我是iOS开发的新手。我正在尝试实现NSURLConnectionDataDelegate协议,但似乎没有一个委托方法被调用过。我必须在自己身上键入委托方法,它应该自动生成吗?
我在每个委托方法中都有一个 NSLog 命令,但没有打印任何内容。我正在使用 NSURLConnection 异步下载并跟踪进度,以便稍后可以更新进度视图。
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协议。我必须创建 connectionDidFinishLoad、didReceiveResponse 和 didReceiveData 方法,但这些方法不会被调用。
我要么没有正确实现协议,要么我将一些方法声明为 +,一些声明为 - (有些方法是类方法,而有些是实例方法)
downloadPDFToMyDocumentsFrom 是一个类方法,当用户单击下载时调用。此方法设置 NSURL 文档,设置 URL 等和委托,并打开文件流以接收数据。但是,不会调用其他方法。
您的downloadPDFToMyDocumentsFrom
方法被设置为类方法(+
),并且您将委托设置为self
,这意味着在这种情况下为类。你应该使downloadPDFToMyDocumentsFrom
方法成为实例方法(-
),以便self是一个实例化的对象。