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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-11-2008, 03:38 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2008
Location: Minneapolis, MN
Posts: 208
Default Check for Wifi?

I am nearly finished with an app that is 95% offline but does have an online component. With the iPhone this is no problem, it can connect anywhere.

But I'd like to check if an iPod Touch user can get to the internet. If not, I'd like to gray out and/or notify the user that the service is unavailable in their area.

If possible, I'd like to check if the user CAN get to the internet, not just if they are connected. Is there a way to programmatically see if they are in range of a wifi network? - again, not just check if they are currently connected to one. I've searched for wireless and wifi in the docs, but nothing useful came up, except, maybe, reachability. But I've seen other apps that do all kinds of wifi functions, including automatically connecting you to an available network.

This isn't a show stopper by any means, but if this is doable I'd love to implement it before I release the app.

Any help is appreciated!
Scuba is offline   Reply With Quote
Old 10-12-2008, 12:26 PM   #2 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

The reachability sample app shows how to do this.
PhoneyDeveloper is offline   Reply With Quote
Old 11-06-2008, 01:10 PM   #3 (permalink)
Registered Member
 
roberthuttinger's Avatar
 
Join Date: Sep 2008
Posts: 96
Default

phoney, youve been a great help in the past. but I have been hacking away at the reachability app for 6 hours now. I stripped the app down so it only swaps an image depending on wifi status. but if I cop the files over accordingly, how can I affect a button in a subview with its own view controller? I have a rootviewcontroller, appdelegate, viewcontroller, and a view. I think this may be an easy question but Im stumped. This should be very easy considering some of the other things ive noodled!

cheers and thanks again phoney! bozie.tehwifistatusbuttonerchangerwanter
roberthuttinger is offline   Reply With Quote
Old 11-06-2008, 02:21 PM   #4 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

I just added NetReachability.m/h to my project. I added the following code to NetReachability. I admit that this is a little cheesy. It's a one time only check but that will work correctly in many cases. I'm intending to change my code to create an instance of NetReachability and set the delgate and update my UI when the reachability status changes. Implementing the delegate callback seems simple.

Code:
+(BOOL)networkIsReachable:(BOOL)inByWiFiOnly
{
	//Make sure we have a WiFi network up & running
	NetReachability*	_reachability = [[NetReachability alloc] initWithDefaultRoute:NO];

	BOOL	result = [_reachability isReachable];
	
	if (inByWiFiOnly)
		result = result && ! [_reachability isUsingCell];

	[_reachability release];

	return result;
}
PhoneyDeveloper is offline   Reply With Quote
Old 11-06-2008, 02:34 PM   #5 (permalink)
Registered Member
 
roberthuttinger's Avatar
 
Join Date: Sep 2008
Posts: 96
Default

so how would I then check for connection in view.m? I just dont know the basic piece of code that calls that function or is it set in a global variable?
so can i do something like if(gloabalvar == 0) then dim the button?

cheers.bo
roberthuttinger is offline   Reply With Quote
Old 11-06-2008, 03:13 PM   #6 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

After adding the code that I showed the following is what I do in my view controller's viewDidLoad:


Code:
	// Check if the network is reachable and turn off the controls if not
	if (! [NetReachability networkIsReachable:NO])
	{
		mStatusLabel.text = NSLocalizedString(@"WiFi is not available", @"no wifi status text in Pull/Push Connectivity");
		mStatusLabel.textColor = [UIColor redColor];
		mGetFileButton.enabled = NO;
		mNetworkIsReachable = NO;
	}
PhoneyDeveloper is offline   Reply With Quote
Old 11-06-2008, 05:17 PM   #7 (permalink)
Registered Member
 
roberthuttinger's Avatar
 
Join Date: Sep 2008
Posts: 96
Default

by NetReachability.h.m you mean the Reachability.h.m file? then if
Code:
if (! [NetReachability networkIsReachable:NO])
is in the view controller i can access t a field in the view from the viewcontroller right... ill try when i get a chance, but now im late for dinner!

thanks a ton :kicks self: learn darn you!

cheers.bo

{added} nope I cant get it to work.. now im late for dinner !

please help me obi wan kenobi, your my only hope...

Last edited by roberthuttinger; 11-06-2008 at 05:35 PM.
roberthuttinger is offline   Reply With Quote
Old 03-11-2010, 11:17 AM   #8 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 13
Default Reachability doesn't do what I need

I have a similar need, but Reachability doesn't seem to do it.

My app is intended for an iPod Touch connecting via WiFi to an adhoc network created by a WiSnap (Wi-Fi RS-232 serial adapter iPhone Android BlackBerry Windows Symbian) that is essentially a WiFi-to-RS232 adapter. The WiSnap [hosts?] the adhoc network on 169.254.1.1.

I need to be able to tell (1) if the WiSnap is powered up and the adhoc network available, (2) if I can actually reach 169.254.1.1, and (3) if my socket connection is working.

Well, I modified the reachability sample program to go to 169.254.1.1 instead of Apple. I'll describe a scenario where, at some prior time, the iPod was connected to the adhoc WiFi network. I turn both iPod and WiSnap power off. I turn iPod back on. I run Reachability. It says that Rmote Host 169.254.1.1 is "Reachable WiFi". It says the same for TCP/IP routing and local WiFi. This is NOT TRUE. The power is still OFF to the WiSnap. There is NO adhoc network in existence.

When I go to Settings on the iPod and look at WiFi, it still lists the WiSnap on the the main Settings page ("WiSnap-3727" is the adhoc network name). If I touch and go to the Wi-Fi Networks view, there's still a check mark net to WiSnap-3727, but the signal strength icon is almost all grayed out.

So, I believe the Settings program knows there's not actually a connection at the moment. But Reachability doesn't.

To further test, I can change WiFi connections to my office LAN, which is obviously different from the WiSnap. When I do this and subsequently run Reachability, it can still find remote host 169.254.1.1 (this must be some OTHER host on my LAN, not the WiSnap. I'm not worrying about this.) It says TCP/IP routing works. However, for Local WiFi, it says "Access Not Available". This is *indeed* what I *expect*.

Now I change WiFi connections back to the WiSnap. Well, I can't do that without turning the WiSnap on. I do so. I connect with Settings app. I immediately turn WiSnap power back OFF before leaving Settings app. I exist Settings and run Reachability. It thinks I can reach WiSnap. So, it's not going 'far enough' determining reachbility.

IN ADDITION TO ALL THIS, what I really need is for my app to assist the user in switching between TWO different adhoc networks. One is the adhoc network between the iPod and the WiSnap. Another is a hypothetical adhoc between the iPod and a lone Laptop. For test purposes, I can use my office WiFi LAN in lieu of the lone Laptop. Either way, I want my app to be able to SWAP connections between the two networks.
Dr.Lightning is offline   Reply With Quote
Old 03-12-2010, 09:18 PM   #9 (permalink)
Registered Member
 
Dynno's Avatar
 
Join Date: Jan 2010
Posts: 83
Default

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.
Dynno is offline   Reply With Quote
Old 06-29-2010, 08:16 AM   #10 (permalink)
el guaje
 
kevinyide's Avatar
 
Join Date: Jun 2010
Posts: 220
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 08:17 AM. Reason: typo
kevinyide is offline   Reply With Quote
Old 07-19-2010, 05:20 AM   #11 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 63
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 05:38 AM.
Ephist is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 158,757
Threads: 89,201
Posts: 380,569
Top Poster: BrianSlick (7,129)
Welcome to our newest member, JamunaDas45
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:17 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0