3

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을 열어 데이터를받습니다. 그러나 다른 메소드 중 아무 것도 호출되지 않습니다.

+0

http://stackoverflow.com/questions/9577317/nsurlconnection-delegate-method/9577548#9577548 – Eric

답변

2

귀하의 downloadPDFToMyDocumentsFrom 메서드는 클래스 메서드 (+)로 설정되어 있으며이 경우 클래스 인 self이되도록 대리인을 설정했습니다. downloadPDFToMyDocumentsFrom 메서드를 인스턴스 메서드 (-)로 만들어서 self가 인스턴스화 된 개체가되도록해야합니다.

+0

의미가 있습니다. 그 상황에서 self는 클래스 이름을 가리키고 그것을 인스턴스 메소드로 변경하면 클래스의 인스턴스를 가리킨다. 나는 그런 느낌 이었지만 모든 것을 바꾸기를 꺼려했습니다. – Bilal