如何简单地测试本地 Wifi 连接到 IP,例如 192.168.0.100 与代码状态? [英] How to simply test a local Wifi Connection to an IP, for example 192.168.0.100 with code status?

查看:78
本文介绍了如何简单地测试本地 Wifi 连接到 IP,例如 192.168.0.100 与代码状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你知道我是否可以简单地测试一下是否有 WIFI 本地连接?例如,如果 url 192.168.0.100 是可访问的.我尝试使用可达性但没有成功.它告诉我它是连接的,但它不是真的.

Do you know if I can simply test if there is a WIFI local connection ? For example if the url 192.168.0.100 is reachable. I tried with the Reachability without success. It tells me that it is connected, but it is not true.

我想先测试是否有本地 WIFI 连接,然后当我确定有连接时,启动该 Web 服务:

I would like first to test if there is a local WIFI connection, and then when I am sure there is a connection, launch that Web Service :

- (void)callWebService:(NSString *)url withBytes:(NSString *) bytes //GET
{
        NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
        NSString *url_string = [bytes stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [request setURL:[NSURL URLWithString:[url stringByAppendingString: url_string]]];
        [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
        [request setTimeoutInterval:timeOut];
        NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; //try NSURLSession
        [connection start];
}

提前致谢.

推荐答案

NSURLConection 有很多委托方法.尝试以下方法之一:

NSURLConection have many delegate methods. try one of the following:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [self.download_connection cancel]; // optional depend on what you want to achieve.
    self.download_connection = nil; // optional

    DDLogVerbose(@"Connection Failed with error: %@", [error description]);
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSInteger state = [httpResponse statusCode];

    if (state >= 400 && state < 600)
    {
        // something wrong happen.
        [self.download_connection cancel]; // optional
        self.download_connection = nil; // optional
    }
}

这篇关于如何简单地测试本地 Wifi 连接到 IP,例如 192.168.0.100 与代码状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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