我目前有一個ap,它試圖根據我正在與之通信的某些服務器打開一個webview。檢查與NSURLConnection連接的有效IP地址
但是,如果iphone/ipad和服務器(或其他設備)不在同一個網絡中,我允許用戶輸入自己的服務器IP。但是,我試圖使用NSURLConnection來檢測是否可以打開與給定IP的連接,但即使服務器地址(甚至是隨機Web地址)完全是假的,NSURLConnection也不會返回錯誤。
的.H
@interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate, UITableViewDataSource, UITableViewDelegate,UIWebViewDelegate, NSURLConnectionDataDelegate> {
在.M
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath
{
dev_ip = @"http://www.asdfasfdfa.com/";
//dev_ip = (random ip)
NSMutableURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:dev_ip]];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
NSLog(@"Connection established");
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"No Device at designated IP"]
delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
此的if/else總是輸出相關的代碼 '已建立連接。'這是NSURLConnection不應該使用的東西嗎?如果是這樣,我可以使用什麼來檢測給定IP處的設備以進行連接。我需要阻止用戶嘗試連接到不好的IP,所以最好的方法是什麼?
您只創建了一個對象來表示連接。您不應該考慮建立連接本身。 –
我覺得這樣的事情正在發生,但我所知道的NSURLConnection的所有代表都適用於接收數據。大多數情況下永遠不會發生。那麼如何檢查建立的連接? – JMD