检查用于与NSURLConnection的连接的有效IP [英] Checking for valid IP for connection with NSURLConnection

查看:33
本文介绍了检查用于与NSURLConnection的连接的有效IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个ap尝试基于与之通信的某些服务器打开Web视图.

I currently have an ap that attempts to open a webview based on certain servers I am communicating with.

但是,如果iphone/ipad和服务器(或其他设备)不在同一网络上,我允许用户键入自己的服务器IP.但是,我试图使用NSURLConnection来检测是否可以打开与给定IP的连接,但是即使服务器地址(甚至是随机网址)完全是虚假的,NSURLConnection也不会返回错误.

However, I allow the user the capability to type in their own server IPs in case both the iphone/ipad and server(or other device) are not on the same network. However, I am attempting to use NSURLConnection to detect if I can open a connection with the given IP however NSURLConnection never returns an error, even if the server address(or even a random web address) is completely bogus.

.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,那么这样做的最佳方法是什么?

this if/else always outputs 'Connection established.' Is this something NSURLConnection should not be used for? If so, what can I use to detect devices at given IP's for connectivity. I need to stop the user from attempting to connect to bad IP's so whats the best method in doing so?

推荐答案

NSURLConnection与委托一起使用时,在连接,无法连接和接收数据时将调用委托方法.您应该查看NSURLConnectionDelegate.

NSURLConnection, when used with delegation, will call delegate methods when it connects, fails to connect and receive data. You should look into NSURLConnectionDelegate.

这是一个简单的例子:

    // In your .h

    @interface MyClass : NSObject <NSURLConnectionDelegate, NSURLConnectionDataDelegate>

    @end

EDIT 您实际上需要两个委托.

EDIT You actually need both delegates.

    // 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

此外,也只需将其丢弃,就不必使用异步调用.您可以发送一个不需要您实现委托的同步调用.方法如下:

Also, just throwing it out there too, you don't necessarily have to use an asynchronous call. You can send a synchronous call which doesn't require you to implement a delegate. Here's how:

    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];

您可以检查响应值和错误.

You can check the response value and errors.

这篇关于检查用于与NSURLConnection的连接的有效IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆