只是想知道下面的代碼...當我關閉我的互聯網連接並運行它,我希望我會在我的控制檯日誌中「連接失敗」。任何人都可以解釋爲什麼我不是?謝謝。NSURLConnection沒有給互聯網連接禁止錯誤?
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");
}
嗯..你能告訴我怎麼可以測試如果它成功了?我試過如果([NSURLConnection canHandleRequest:request]),但似乎沒有工作 – sayguh 2012-03-22 13:19:08
假設你發送一個異步請求,你需要實現委託方法'connection:didFailWithError:'。 – Jim 2012-03-22 14:53:38
感謝您的信息吉姆。重新:下面的評論.. didReceiveData似乎工作,即使WiFi關閉。這就是我所說的緩存。我現在看到,即使頁面返回404,它也會收到數據。您能否告訴我常用的方法是確保您返回了您要查找的完整文件? – sayguh 2012-03-22 16:39:00