Quote:
Originally Posted by Dynno
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.
|
Hey A.J this works great! though i have a problem. im using a Mac connected via an Ethernet cable. If i start an app in the simulator with the ethernet cable disconnected, it will return me a error msg saying wifi is needed etc. thats great.
Now i tried another scenario, i connected the ethernet cable and started the app. so i clicked the button(button which has the error checking above) and of course it didnt return error, but here i disconnected the ethernet cable ( presumingly to emulate a lost connection ) . and i get a ***Terminating app due to uncaught exception 'NSRangeException'.
im thinking that since the app was started up with BOOL that wifi = true. and when i disconnected the ethernet cable, the wifi supposedly is false. so how can i update the BOOL?
*Pls note , this situations are all during usage of the application in the simulator.
*UPDATE , something i did makes it messed up. my bad for posting.