문제

인터넷에서 일부 데이터를 다운로드하기를 원합니다. iPhone SDK 문서에서 NSURLConnection 클래스를 찾았습니다. 다운로드에 사용됩니다. 나는 문서에서와 같은 코드를 작성하여 실행했습니다. 연결이 성공적으로 생성되었지만 데이터가 다운로드되지 않았습니다. ConnectionDidFinishLoading은 SEC 또는 2 후에는 발사되지만 결과는 결과가 없습니다. 문제는 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];
}
도움이 되었습니까?

해결책

당신은 "수신"을 놓치고 있습니다 :

// Your signature
- (void)connection:(NSURLConnection *)connection didRecieveData:(NSData *)data;

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

다른 팁

당신은 당신의 didreceivedata 메소드의 이름으로 오타가 있습니다 (i 이전 e, c :-).

따라서 클래스가 (선택 사항) 선택기를 구현하지 않는 것처럼 보이며 조용히 무시됩니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top