可达性帮助 - WiFi 检测

发布于 2024-11-30 08:08:43 字数 1957 浏览 4 评论 0原文

我已将 Reachability 导入到我的应用程序中,并且有几个操作方法问题要问大家。首先让我解释一下我的应用程序和其他工具。

该应用程序同时与两个事物进行通信:即席网络和通过 3G 的互联网。注意:ad-hoc 网络未连接到互联网。这工作得很好——它已经被实现并且测试得很好。

话虽如此,我想实现 Reachability 来检测两件事。

1) 用户是否连接到 wifi ad-hoc 网络? (如果可能的话,更好的是检测它是否连接到前缀为 WXYZ 的 wifi ad-hoc 网络。例如,如果列出了两个网络,一个称为 Linksys,另一个称为 WXYZ-Testing_Platform,它就知道无论是否连接到 WXYZ)。

2)用户可以通过3G(或2G等)连接到互联网并访问我们的服务器吗?

提前致谢

编辑以包括未来关注者的答案:

对于 1),我的代码如下所示:

.h
#import <SystemConfiguration/CaptiveNetwork.h> //for checking wifi network prefix

.m
- (BOOL) connectedToWifi
{

    CFArrayRef myArray = CNCopySupportedInterfaces();
    // Get the dictionary containing the captive network infomation
    CFDictionaryRef captiveNtwrkDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));

    NSLog(@"Information of the network we're connected to: %@", captiveNtwrkDict);

    NSDictionary *dict = (__bridge NSDictionary*) captiveNtwrkDict;
    NSString* ssid = [dict objectForKey:@"SSID"];

    if ([ssid rangeOfString:@"WXYZ"].location == NSNotFound || ssid == NULL)
    {
        return false;
    }
    else
    {
        return true;
    }
}

对于 2),我导入了 Reachability每当我连接到服务器时都使用此方法...注意:替换 http://www.google.com 带有服务器信息

-(void) checkIfCanReachServer
{
UIAlertView *errorView;
    Reachability *r = [Reachability reachabilityWithHostName:@"http://www.google.com"];
    NetworkStatus internetStatus = [r currentReachabilityStatus];


    if(internetStatus == NotReachable) {
        errorView = [[UIAlertView alloc] 
                     initWithTitle: @"Network Error" 
                     message: @"Cannot connect to the server." 
                     delegate: self 
                     cancelButtonTitle: @"OK" otherButtonTitles: nil];  
        [errorView show];
    }
}

I have imported Reachability into my application, and I have a couple of how-to questions for you all. Let me explain my application and other tools first.

This application communicates with two things AT THE SAME TIME, an ad-hoc network, and the internet through 3G. Note: The ad-hoc network IS NOT connected to the internet. This works perfectly - it's already implemented and tests out wonderfully.

With that being said, I want to implement Reachability to detect a two things.

1) Is the user connected to a wifi ad-hoc network? (Even better, if possible, is to detect if it is connected to the wifi ad-hoc network with a prefix of WXYZ. For example, if there are two networks listed, one called Linksys and the other called WXYZ-Testing_Platform, it knows whether or not it is connected to WXYZ).

2) Can the user connect to the internet through 3G (or 2G, etc) and access our server?

Thanks in advance

EDIT TO INCLUDE ANSWER FOR FUTURE LOOKERS:

For 1), my code looks like this:

.h
#import <SystemConfiguration/CaptiveNetwork.h> //for checking wifi network prefix

.m
- (BOOL) connectedToWifi
{

    CFArrayRef myArray = CNCopySupportedInterfaces();
    // Get the dictionary containing the captive network infomation
    CFDictionaryRef captiveNtwrkDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));

    NSLog(@"Information of the network we're connected to: %@", captiveNtwrkDict);

    NSDictionary *dict = (__bridge NSDictionary*) captiveNtwrkDict;
    NSString* ssid = [dict objectForKey:@"SSID"];

    if ([ssid rangeOfString:@"WXYZ"].location == NSNotFound || ssid == NULL)
    {
        return false;
    }
    else
    {
        return true;
    }
}

And for 2), I imported Reachability and have it using this method whenever I go to connect to the server... NOTE:replace http://www.google.com with the server information

-(void) checkIfCanReachServer
{
UIAlertView *errorView;
    Reachability *r = [Reachability reachabilityWithHostName:@"http://www.google.com"];
    NetworkStatus internetStatus = [r currentReachabilityStatus];


    if(internetStatus == NotReachable) {
        errorView = [[UIAlertView alloc] 
                     initWithTitle: @"Network Error" 
                     message: @"Cannot connect to the server." 
                     delegate: self 
                     cancelButtonTitle: @"OK" otherButtonTitles: nil];  
        [errorView show];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2024-12-07 08:08:43

可达性仅让您知道设备是否可以成功发送数据包。所以
对于 1),您应该参考 iPhone 在没有私有库的情况下获取 SSID。对于 2),您将仅使用 Reachability 来检查互联网连接,然后您需要使用 NSURLConnection 或其他网络库来确保您可以访问您的服务器。

Reachability only lets you know if the device can send data packets out successfully. So
for 1) you should refer to iPhone get SSID without private library. For 2) you will use Reachability only to check for an internet connection then you would need to use NSURLConnection or other network library to make sure you can reach your server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文