iOS 개발을 처음 사용합니다. NSURLConnectionDataDelegate 프로토콜을 구현하려고하지만 대리자 메서드가 호출되지 않습니다. 대리자 메서드를 직접 입력해야했습니다. 자동으로 생성되기로되어 있습니까?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 파일 : 나는 자기에있는 NSURLConnection의 URLConnection의 대리자를 설정 한
#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);
}
SearchFeed.m 클래스입니다 SearchFeed.h에서 NSURLConnectionDataDelegate 프로토콜을 구현하려고했습니다. connectionDidFinishLoading, didReceiveResponse 및 didReceiveData를 작성해야했습니다. 메서드를 호출하지만 이러한 메서드는 호출되지 않습니다.
I 중 하나를 제대로 프로토콜을 구현하지 않은 또는 좀 + 등의 방법 등을 선언 한 일부 - (일부는 인스턴스 메소드 동안 몇 가지 방법은 클래스 메소드입니다)
downloadPDFToMyDocumentsFrom가 호출되는 클래스 메서드 때 사용자가 다운로드를 클릭합니다. 이 메서드는 NSURLConnection을 설정하고 URL 등을 위임자로 설정하고 fileStream을 열어 데이터를받습니다. 그러나 다른 메소드 중 아무 것도 호출되지 않습니다.
http://stackoverflow.com/questions/9577317/nsurlconnection-delegate-method/9577548#9577548 – Eric