iPhone 응용 프로그램 설명서에서 인터넷에서 데이터를 다운로드하고 싶습니다. 다운로드에 사용되는 NSURLConnection 클래스가 있습니다. 맞습니까? 문서에있는 것과 동일한 코드를 작성하여 실행했습니다. 연결이 성공적으로 생성되었지만 데이터가 다운로드되지 않았습니다. connectionDidFinishLoading은 초 또는 2 초 후에 발생하지만 결과에는 데이터가 없습니다. 문제는, didRecieveData 메소드가 결코 실행되지 않는다는 것입니다. 나는 왜 인터넷을 검색했는지는 모르지만 모든 결과는 문서에있는 것과 동일한 코드입니다. 제발 조언 좀 해주실 래요? 모든 답장을 보내 주셔서 감사합니다 내 다운로더 클래스 소스 코드가 아래에 있습니다.NSURLConnection이 didRecieveData 메서드를 호출하지 않습니다.
Downloader.h
@interface Downloader : NSObject {
NSURLConnection *conn;
//Array to hold recieved data
NSMutableData *recievedData;
}
@property (nonatomic, retain) NSURLConnection *conn;
@property (nonatomic, retain) NSMutableData *recievedData;
- (void)downloadContentsOfUrl:(NSURL *)url;
@end
Downloader.m는
#import "Downloader.h"
@implementation Downloader
@synthesize recievedData, conn;
- (void)connection:(NSURLConnection *)connection didRecieveResponse:(NSURLResponse *)response
{
NSLog(@"did recieve response");
[recievedData release];
recievedData = nil;
}
- (void)connection:(NSURLConnection *)connection didRecieveData:(NSData *)data
{
NSLog(@"did recieve data");
//Append the new data to the recieved data
[recievedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//Release the connection and the data object
[connection release];
[recievedData release];
NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//ToDo with data
//[recievedData writeToFile:@"data" atomically:YES];
NSLog(@"downloaded");
NSLog(@"%u", [recievedData length]);
//Release the connection and the data object
[connection release];
[recievedData release];
}
- (void)downloadContentsOfUrl:(NSURL *)url
{
//Create the connection
//Create the request
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
//Create the connection with the request and start loading the data
conn = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self
startImmediately:YES];
if(conn)
{
//Create the NSMutableData that will hold the recieve data
recievedData = [[NSMutableData data] retain];
NSLog(@"Connection success!");
}
else
{
NSLog(@"Can't download this file!");
}
}
- (void)dealloc
{
[conn release];
[recievedData release];
[super dealloc];
}