
Originally Posted by
Jirka Jirout
How do you check for internet availability? Ping to some pre-defined host?
Well, I had tried a few different ways at first. The first way I tried was using:
Code:
if(system("ping -c 1 -t 3 www.google.com")) return NO; else return YES;
However, I had problems with this. If the internet was available, it would be ok. Then with the app still running, if I removed internet access it would show the internet not being accessible (everything seems ok so far.) But then when I re-enable the internet (and give it time, like I can load google.com in Safari), when this method runs it says "cant resolve host google.com".. Odd!
So I looked on developer.apple.com, and saw they suggested using this:
Code:
#include <CoreFoundation/CoreFoundation.h>
#include <SystemConfiguration/SCNetworkReachability.h>
- (BOOL)wirelessHasInternet
{
SCNetworkReachabilityRef netreach;
SCNetworkConnectionFlags flags;
netreach = SCNetworkReachabilityCreateWithName (
kCFAllocatorSystemDefault, "www.google.com" )
AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER;
SCNetworkReachabilityGetFlags ( netreach, &flags )
AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER;
BOOL reachable = (kSCNetworkFlagsReachable & flags);
if (reachable) return YES; else return NO;
}
However.. This didn't work. I use Airport and connect to a network
that I can access the internet on, and it says it can't access the
internet. And if I try it on a network that I can't access the
internet on, it still says it can't access it.
So, then I toyed around and looked through more docs and came up with this:
Code:
if ([[preferencesDictionary valueForKey:@"CHECK_FOR_INTERNET"] isEqualToString:@"YES"])
{
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:3.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {}
}
And implement its' delegate methods:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[self setInternetAccess:[[NSBundle mainBundle] pathForImageResource:@"internet_ok.png"]];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[self setInternetAccess:[[NSBundle mainBundle] pathForImageResource:@"internet_bad.png"]];
}
That's what I use now. For me, it works. When testing, I had to make sure to disable my ethernet connection, and only enable my wireless. The if (connection) {} line is a placeholder.
The problems with what I'm using now is:
1. If there is internet, it shows it's connected. But if you connect to one of those pay WiFi hot-spots, like in airports or hotels, it still shows you are connected, because the hotspot routes all requests to the same place. However, dave (gnat) told me how to fix that, by comparing Googles, (or Apple.com's) header of the request to what it should be (hopefully it doesnt change), and then that would let me know better if there is actual "internet" access. Perhaps the light could be orange if it was a pay internet site. But.. I haven't done this yet 
2. Someone said it may pop-up a dialup-internet setting if you have one, as it tries to access the net.. My response is "who the hell would be using a dialup internet connection on their Mini?"
Besides that,, it works flawlessly! And, I like this method because of how the delegate methods handle the actual interpretation of the result. And I have the first two lines just in a timer that fires every 5 seconds or so. (that may be a bit too frequent..)
Bookmarks