Hello All,
I'm having an interesting issue with reachability (or maybe my ability to figure out what to do) :)
Using several pieces of code from different places and including apple's reachability example, I'm experiencing a funny behavior... when I turn off the WIFI, the code returns a non reachable server.. I get that!!! BUT when I use the server as w.google.com, it says it's reachable...
Can anyone help me?
1) I would like to determine what WIFI name I'm connected to? (if it's not the proper corporate one, bail... )
2) if the server I need to connected to (FOOSERVER) consume services from is not reachable, bail...
3) if there's no WIFI signal, bail...
I've tried all sorts of things but I cant seems to make it work correctly!!
======== ConnectionVerification.h =========
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <netinet/in.h>
#import <arpa/inet.h>
#import <netdb.h>
@interface ConnectionVerification : NSObject {
}
+ (NSString *) localIPAddress;
+ (NSString *) getIPAddressForHost: (NSString *) theHost;
+ (BOOL) hostAvailable: (NSString *) theHost;
+ (BOOL) addressFromString:(NSString *)IPAddress address:(struct sockaddr_in *)address;
+ (BOOL) isConnected;
\\\\ ConnectionVerification.m \\\\\
+ (BOOL) isConnected {
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
// Recover reachability flags
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags) {
NSLog(@"Error. Could not recover network reachability flags");
return NO;
}
BOOL isReachable = flags & kSCNetworkFlagsReachable;
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
NSURL *testURL = [NSURL URLWithString:@"http://w.google.com"];
NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0];
NSURLConnection *testConnection = [[[NSURLConnection alloc] initWithRequest:testRequest delegate:self] autorelease];
NSLog(@" isReachable = %@", isReachable? @"YES":@"NO");
NSLog(@" needsConnection = %@", needsConnection? @"YES":@"NO");
NSLog(@" nonWiFi = %@", nonWiFi? @"YES":@"NO");
BOOL temp = [self hostAvailable:@"w.google.com"];
NSLog(@" temp = %@", temp? @"YES":@"NO");
return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO;
}
\\\\ hostAvailable \\\\\ From Erica Sadun Cookbook....
+ (BOOL) hostAvailable: (NSString *) theHost {
NSString *addressString = [self getIPAddressForHost:theHost];
if (!addressString)
{
printf("Error recovering IP address from host name\n");
return NO;
}
struct sockaddr_in address;
BOOL gotAddress = [self addressFromString:addressString address:&address];
if (!gotAddress)
{
printf("Error recovering sockaddr address from %s\n", [addressString UTF8String]);
return NO;
}
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&address);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags)
{
printf("Error. Could not recover network reachability flags\n");
return NO;
}
BOOL isReachable = flags & kSCNetworkFlagsReachable;
return isReachable ? YES : NO;;
}
WITHOUT WIFI
(gdb) continue
2010-03-17 10:08:05.385 test[2326:207] isReachable = NO
2010-03-17 10:08:05.392 test[2326:207] needsConnection = NO
2010-03-17 10:08:05.396 test[2326:207] nonWiFi = NO
resolv: Unknown host
Error recovering IP address from host name
2010-03-17 10:08:05.416 test[2326:207] temp = NO
WITH WIFI
2010-03-17 10:08:51.800 test[2335:207] isReachable = YES
2010-03-17 10:08:51.809 test[2335:207] needsConnection = NO
2010-03-17 10:08:51.813 test[2335:207] nonWiFi = NO
2010-03-17 10:08:51.831 test[2335:207] temp = YES
2010-03-17 10:08:51.838 test[2335:207] Connection OK