이 클래스와 연결된보기를로드 할 때 대리자 메서드를 호출 할 수없는 것 같습니다. 그 중 하나도 실행되지 않습니다. 내가 간과 해 왔던 것이 겠지만, 내 삶은 그 이유를 알 수 없습니다.어떻게 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...
필수는 아니지만 클래스를 선언하면 'NSURLConnection'의 프로토콜 중 하나를 준수하지 않습니다. –
이것은 분명하게 들릴지 모르지만, 나는 이상한 것들을 보았다. 실제로이 클래스의 객체를 연결 대리자로 설정 했습니까? – Abizern
NSURLConnection을 처음 사용했습니다. 무슨 뜻인지 모르겠다. (나는 이것을 오랫동안 해왔다.) –