2012-09-26 37 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。