2012-05-13 2 views
0

와 연결 :이 코드를 사용하고 NSURLRequest

NSString *recievedData; 

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.site.com/"] 
              cachePolicy:NSURLRequestUseProtocolCachePolicy 
             timeoutInterval:60.0]; 
// create the connection with the request 
// and start loading the data 
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
if (theConnection) { 
    // Create the NSMutableData to hold the received data. 
    // receivedData is an instance variable declared elsewhere. 
    recievedData = [NSMutableData data]; 

    NSLog(@"%@", recievedData); 
} else { 
    // Inform the user that the connection failed. 
    NSLog(@"Unsuccessful."); 
} 

그것은 this의 수정 된 버전입니다.

내 문제는 항상 내가 인터넷에 연결되어있어 여부, 항상 성공으로가는 여부

<>

를 반환한다는 것입니다.

답변

0

데이터를받지 못했습니다.받은 데이터를 보유 할 개체를 인스턴스화했습니다. 응답 및 실패를 처리하기위한 위임 메서드를 구현해야하며 대개 NSURLConnection을 비동기 적으로 사용하는 것이 가장 좋습니다.

몇 가지 예제 코드가 있습니다. Using NSURLConnection

0

NSURLConnection이 그렇게 작동하지 않습니다. 연결을 시작한 다음 데이터가 수신되면 콜백을받습니다.

간단한 호출로 원격 데이터를 검색하려면 NSData의 dataWithContentsOfURL 메서드를 사용하십시오. 그러나 보조 스레드에서 사용해야합니다. 그렇지 않으면 호출 기간 동안 사용자 인터페이스를 잠그고 시스템이 너무 오래 걸리면 앱이 종료 될 수 있기 때문입니다.

전체 코드는 NSURLConnection example입니다.