1

以下のこのコードについて疑問に思っています...インターネット接続をオフにして実行すると、コンソールログに「接続に失敗しました」と表示されると予想していました。なぜ私がいないのか誰かが説明できますか?ありがとう。

NSString *urlString = [NSString stringWithFormat:@"http://www.myurl.com/RSS/feed.xml"];

NSURL *serviceURL = [NSURL URLWithString:urlString];

//Create the request
NSURLRequest *request = [NSURLRequest requestWithURL:serviceURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];

//Create the connection and send the request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];


//Make sure the connection is good
if (connection) {
    //instantiate the responseData structure to store the response
    self.responseData = [NSMutableData data];

}
else {
    NSLog(@"Connection failed");
}
4

2 に答える 2

1

接続オブジェクトの作成ではなく、接続自体が失敗したかどうかを確認したい場合は、次のようにデリゲートを使用します。

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
        NSLog("Oh noes D=");
}
于 2012-03-22T13:19:30.370 に答える
1

あなたはまだ実際にリクエストを試みていません。 if (connection)リクエストが成功したかどうかはテストせず、接続を表すオブジェクトを作成できたかどうかをテストするだけです。リクエストを行うには、そのメソッドの1つを呼び出す必要があります。詳細については、ドキュメントを参照してください。

于 2012-03-22T13:06:24.570 に答える