2012-03-29 4 views
0

asynchornus 요청을 구현하려고합니다. 나는 내 연구를하고이 내가 가진 최고의하지만 코드는 내가 내 .H 파일에 NSURLConnectionDelegate을 추가 한비동기 요청 생성

NSURL *url2 = [NSURL URLWithString:@"www.google.com"]; 

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url2]; 
    NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 

    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
    { 
     if ([data length] > 0 && error == nil) 
      [delegate receivedData:data];//i get the error here use of undeclared identifier 'delegate' ,when I put self insead I receive the error : no visible @interface for "my class name" declares the selector "received data" 
     else if ([data length] == 0 && error == nil) 
      [delegate emptyReply];//same error here 
     else if (error != nil && error.code == ERROR_CODE_TIMEOUT) //the error here is "use of undelcared identifier ERROR_CODE_TIMEOUT 
      [delegate timedOut];//error here is save as the first one 
     else if (error != nil) 
      [delegate downloadError:error];//error here is save as the first one 
    }]; 

에 대한 해결책을 찾을 수없는 오류로 가득하다

누군가는 말해 줄 수 오류가 무엇입니까? 필요에 따라 대리자 메서드를 구현 : 대표 : initWithRequest -

덕분에

+0

당신이 당신의 결과를 붙여 주실 래요? –

답변

1

이 방법에 대한 경험이 없지만이 예제가 효과적입니다. 인앱 구매에 대한 제품 정보를 테스트하기 위해 이것을 내 서버로 보냈습니다. 게시 된 것처럼 다른 가능한 결과를 테스트하기 위해 더 많은 다른 if-if를 넣을 수도 있지만 시작해야합니다. 내가 왜 스텁에 위임자에 대한 참조를 게시했는지 확신 할 수 없습니다. 블록 메소드의 요점 (또는 포인트 중 하나)은 위임자없이 이러한 종류의 작업을 수행 할 수 있도록하는 것입니다.

- (void)makeConnection { 
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:kServerPath] 
              cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
             timeoutInterval:5]; 
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
[NSURLConnection sendAsynchronousRequest:theRequest queue:queue completionHandler:^(NSURLResponse* theResponse, NSData* theData, NSError* error) { 
    NSLog(@"%@, %@ %@",theResponse.suggestedFilename,theResponse.MIMEType,theResponse.textEncodingName); 
    if (theData != nil && error == nil) { 
     NSArray *productArray = [NSJSONSerialization JSONObjectWithData:theData options:NSJSONReadingMutableContainers error:nil]; 
     NSLog(@"%@",productArray); 
    }else{ 
     NSLog(@"%@",error.localizedDescription); 
    } 
}]; 

}

0

나는 그냥 할 쉽게 찾을. 당신은 적어도

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

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

을 할 것입니다. 일부 코드는 http://developer.apple.com/library/ios/#samplecode/SimpleFTPSample/Listings/URLGetController_m.html을 참조하십시오. 수신 된 데이터는 증분 블록에 대해 호출됩니다. 속성의 총 데이터를 추적하고 도착한대로 데이터를 추가하려고합니다. 그것 이외에, 당신은 오류 처리 및 인증을 던질 수 있지만, 시작해야합니다.