I had a similar problem a while back and an awesome individual helped me out with it, so I'll return the favor to someone else. In this particular case you'll be checking if you're connected to a WiFi network. It will not test to see if you actually have access to the internet. You need to implement different means of testing for that (for example if you are using UIWebView's, you can use didFailLoadWithError method to take care of that.)
1) Import Reachability.h/m into your project
2) Add the SystemConfiguration Framework to your project
3) In your AppDelegate.h file, #import Reachability.h add the following method declaration.
Code:
-(bool)connectedToWiFi;
4) Now lets implement this method we declared. Within your AppDelegate.m file, add the following code:
Code:
-(bool)connectedToWiFi
{
Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
bool result = false;
if (internetStatus == ReachableViaWiFi)
{
result = true;
}
return result;
}
5) Now depending on what you want to do if there is WiFi connection or not, there are several ways to go from here. The most common way of implementing the result is within the applicationDidFinishLaunching method within your AppDelegate.m file. In order to do that, just put in a simple if statement like such:
Code:
if( [self connectedToWiFi] )
{
NSLog(@"It works");
}
else
{
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"No WiFi Connection" message:@"This App requires internet\n connection via WiFi. Please connect to a WiFi network\n and try again" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[myAlert show];
[myAlert release];
}
Edit: Holy crap this is an old thread! We have some thread diggers in this forum. lol
Best regards,
-A.J.