【问题标题】:Implementing NSURLConnectionDataDelegate Protocol实现 NSURLConnectionDataDelegate 协议
【发布时间】:2013-09-05 21:07:43
【问题描述】:

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

【问题讨论】:

标签: iphone ios delegates nsurlconnection nsurlconnectiondelegate


【解决方案1】:

您的 downloadPDFToMyDocumentsFrom 方法设置为类方法 (+),并且您将委托设置为 self,在这种情况下表示类。您应该将 downloadPDFToMyDocumentsFrom 方法设为实例方法 (-),以便 self 成为实例化对象。

【讨论】:

  • 这是有道理的。在该上下文中的 self 指向类名,而将其更改为实例方法则指向该类的实例。我有一种感觉,就是这样,但我不愿意改变一切。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-16
  • 2015-11-02
相关资源
最近更新 更多