Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reachability Help - WiFi Detection

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];
    }
}
like image 533
Baub Avatar asked Aug 19 '11 14:08

Baub


1 Answers

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.

like image 84
Joe Avatar answered Oct 12 '22 00:10

Joe