2012-09-26 51 views
3

我目前有一個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,所以最好的方法是什麼?

+0

您只創建了一個對象來表示連接。您不應該考慮建立連接本身。 –

+0

我覺得這樣的事情正在發生,但我所知道的NSURLConnection的所有代表都適用於接收數據。大多數情況下永遠不會發生。那麼如何檢查建立的連接? – JMD

回答

3

NSURLConnection與代理一起使用時,它會在連接時調用委託方法,無法連接和接收數據。你應該看看NSURLConnectionDelegate。

這裏有一個簡單的例子:

// In your .h 

    @interface MyClass : NSObject <NSURLConnectionDelegate, NSURLConnectionDataDelegate> 

    @end 

編輯你實際上需要雙方的代表。


// In your .m 

    @implementation MyClass 

    - (void)myMethodToConnect { 

     NSString *dev_ip = @"http://74.125.137.101"; // Google.com 

     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:dev_ip]]; 

     NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    } 

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

     switch ([(NSHTTPURLResponse *)response statusCode]) { // Edited this! 
      case 200: { 
       NSLog(@"Received connection response!"); 
       break; 
      } 
      default: { 
       NSLog(@"Something bad happened!"); 
       break; 
      } 
     } 

    } 

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
     NSLog(@"Error connecting. Error: %@", error); 
    } 
    @end 

而且,只是把它扔在那裏過,你不一定必須使用異步調用。您可以發送不需要您實施委託的同步呼叫。方法如下:

NSString *dev_ip = @"http://www.asdfasdfasdf.com/"; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:dev_ip]]; 
    NSURLResponse *response = nil; 
    NSError *error = nil; 

    NSData *connectionData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

您可以檢查響應值和錯誤。

+0

我會嘗試'接收回應'代表。在您的switch語句中,對於您的交換機,響應的類型不是Int類型的NSURLResponse。轉換會改變數據嗎?我假設200值是特定於成功連接的? – JMD

+0

要獲取實際通過的數據,您需要使用didReceiveData方法。如果您願意,我也可以將它包含在代碼中。 –

+0

我很熟悉didReceiveData委託。我只需要找出如何檢測出良好的連接。 – JMD

2

有一個更好的方法來測試服務器是否有效。爲此,Reachability類提供了良好的API。