我是 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 以接收数据。但是,没有调用其他方法。