檢查與NSURLConnection的有效IP

[英]Checking for valid IP for connection with NSURLConnection


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

我目前有一個ap,它試圖基於我正在與之通信的某些服務器打開一個webview。

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.

但是,我允許用戶在iphone/ipad和服務器(或其他設備)不在同一網絡的情況下,在自己的服務器IPs中輸入。但是,我正在嘗試使用NSURLConnection來檢測我是否可以使用給定的IP打開一個連接,但是NSURLConnection不會返回一個錯誤,即使服務器地址(甚至一個隨機的web地址)完全是假的。

the .h

. h的

        @interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate, UITableViewDataSource, UITableViewDelegate,UIWebViewDelegate, NSURLConnectionDataDelegate> {

the relevant code in the .m

在。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];
        }
      }

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?

如果/else總是輸出連接。“這是不應該用的東西嗎?”如果是的話,我可以用什么來檢測給定IP上的設備的連接。我需要阻止用戶嘗試連接壞的IP,所以最好的方法是什么?

2 个解决方案

#1


3  

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

NSURLConnection與委托一起使用時,將在連接時調用委托方法,無法連接和接收數據。您應該查看NSURLConnectionDelegate。

Here's a quick example:

這里有一個簡單的例子:

    // In your .h

    @interface MyClass : NSObject <NSURLConnectionDelegate, NSURLConnectionDataDelegate>

    @end

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.

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

#2


2  

There's a better way to test whether a server is valid or not. The Reachability class provides good API for this purpose.

有一種更好的方法可以測試服務器是否有效。可達性類為此目的提供了良好的API。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/09/26/7209db0fa59e6aa4c3fd326d41fffb54.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com