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 06-29-2010, 07:16 AM   #10 (permalink)
kevinyide
el guaje
 
kevinyide's Avatar
 
Join Date: Jun 2010
Posts: 243
kevinyide is on a distinguished road
Send a message via Yahoo to kevinyide
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.
Hi A.J,
Much thanks for your response!
The Reachability sample contains the
Code:
reachabilityForLocalWiFi
method implemented. Wouldn't that be sufficient to check if the iPhone is connected to a WiFi connection or not? I'm confused a little over the use of
Code:
reachabilityWithHostName
instead of
Code:
reachabilityForLocalWiFi
Perhaps it would be better to use reachabilityForLocalWiFi first to check if the iPhone is connected to a WiFi connection/router and then if TRUE, do a reachabilityWithHostName to check if the connection/router is active on the internet?? I'm sure the reachabilityForLocalWiFi is written to concretely notify of a WiFi connection. Correct me if im wrong. I need to check for only WiFi connectivity for an app im making, hence the query.
Thanks,
Kevin.

Last edited by kevinyide; 06-29-2010 at 07:17 AM. Reason: typo
kevinyide is offline   Reply With Quote
 

» Advertisements
» Online Users: 359
18 members and 341 guests
baja_yu, brainspoon, Creativ, cristofercolmbos, Davor, Domele, iconomania, jbro, mediaspree, Meoz, NSString, Paul Slocum, speed4mee, spiderguy84, toolifog, TylerBetable, UconnAggie
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,551
Threads: 94,078
Posts: 402,728
Top Poster: BrianSlick (7,990)
Welcome to our newest member, hongto24
Powered by vBadvanced CMPS v3.1.0

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