Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

Thread: Check for Wifi?
View Single Post
Old 07-19-2010, 04:20 AM   #11 (permalink)
Ephist
Registered Member
 
Join Date: May 2010
Posts: 63
Ephist is on a distinguished road
Default

Quote:
Originally Posted by Dynno View Post
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.

Last edited by Ephist; 07-19-2010 at 04:38 AM.
Ephist is offline   Reply With Quote
 

» Advertisements
» Online Users: 384
12 members and 372 guests
2Apps1Day, Bryan1, cgokey, Domele, evilmatt10, flamingliquid, ipodphone, Miliguel1, nguyenhieu129, NSString, Objective Zero, williamWang
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,565
Threads: 94,080
Posts: 402,749
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Miliguel1
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:48 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.