2013-06-10 1 views
1

이 클래스와 연결된보기를로드 할 때 대리자 메서드를 호출 할 수없는 것 같습니다. 그 중 하나도 실행되지 않습니다. 내가 간과 해 왔던 것이 겠지만, 내 삶은 그 이유를 알 수 없습니다.어떻게 NSURLConnection 대리자 메서드를 트리거 할 수 있습니까?

DownloadUpdates.h

#import <UIKit/UIKit.h> 

@interface DownloadUpdates : UIViewController 

    @property (strong, nonatomic) NSMutableData *responseData; 
    @property (strong, nonatomic) NSURLConnection *connection; 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error; 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection; 

@end 

DownloadUpdates.m

의 URL은 개인 정보 보호의 목적으로 제거되었지만 그냥 JSON 데이터를 반환 할 것 API를 호출하는 것. 이 URL은 예상대로 작동하므로 코드 관련 문제입니다.

#import "DownloadUpdates.h" 

@interface DownloadUpdates() 

@end 

@implementation DownloadUpdates 

- (void)connection:(NSURLConnection *)_connection didReceiveResponse:(NSURLResponse *)response 
{ 
    _responseData = [[NSMutableData alloc] init]; 
    NSLog(@"Response received"); 
} 

- (void)connection:(NSURLConnection *)_connection didReceiveData:(NSData *)data 
{ 
    [_responseData appendData:data]; 
     NSLog(@"Data received"); 
} 

- (void)connection:(NSURLConnection *)_connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"Unable to fetch data"); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)_connection 
{ 
    NSLog(@"Succeeded! Received %d bytes of data",[_responseData 
                length]); 
// NSString *txt = [[NSString alloc] initWithData:_responseData encoding: NSASCIIStringEncoding]; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSURL *myURL = [NSURL URLWithString:@"URL HERE"]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60]; 

    _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    // Do any additional setup after loading the view. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

제공할만한 도움이나 조언을 보내 주시면 감사하겠습니다.

self.responsedata = [[NSMutableData alloc] init]; 
(.....) 
self.connection = [[NSURLConnection alloc] initWithRequest: ....etc... 
+3

필수는 아니지만 클래스를 선언하면 'NSURLConnection'의 프로토콜 중 하나를 준수하지 않습니다. –

+0

이것은 분명하게 들릴지 모르지만, 나는 이상한 것들을 보았다. 실제로이 클래스의 객체를 연결 대리자로 설정 했습니까? – Abizern

+0

NSURLConnection을 처음 사용했습니다. 무슨 뜻인지 모르겠다. (나는 이것을 오랫동안 해왔다.) –

답변

2

사용에게 속성이 강한 수정을 적용합니다. 예를 들어, 스토리 보드 장면의 클래스로 DownloadUpdates을 지정하지 않은 경우이 문제가 발생할 수 있습니다.

+0

이것은 더 나은 형태이지만 문제가 어떻게 해결되는지는 알 수 없습니다. – danh

+0

@danh :'viewDidLoad'가 끝나자 마자 연결 메소드는 범위를 벗어나서 할당이 해제됩니다. 그것을 강력하게 유지함으로써, 그것은 주변에 머 무르며 대리자 방법을 호출 할 것이다. –

+0

당신은 맞습니다, ARC 이전이지만 재산은 강력하다고 선언되었습니다. 합성 된 setter를 사용하는 것이 더 좋지만 ARC는 속성에 직접 액세스하는 모든 위치에 retain을 삽입합니다. 당신의 대답은 2 년 전만해도 좋았지 만 더 이상은 아니 었습니다. – danh

0

나는이 모든에서 호출지고 있는지 확인 viewDidLoadNSLog 또는 중단 점을 퍼팅 좋을 것 :

관련 문제