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.
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
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;
}
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?
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;
}
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.
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:
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:
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
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:
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.