views:

887

answers:

1

I am using Reachability sample code from Apple site. I have added Reachability.h and Reachability.m files to my project, also I have added SystemConfiguration.framework. Added #import "Reachability.h" to my view controller .m file and declared reachability variable in my view controller .h file. Everything compiles without errors until I start using Reachability instances.

I get ReachableViaWiFiNetwork and ReachableViaCarrierDataNetwork undeclared error.

Why does this happen?

reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];

if ((internetStatus != ReachableViaWiFiNetwork) && (internetStatus != ReachableViaCarrierDataNetwork))
{
}
+3  A: 

Reachability.h enumerates three types: NotReachable, ReachableViaWiFi and ReachableViaWWAN.

These are the values you want to check against, not ReachableViaWiFiNetwork or ReachableViaCarrierDataNetwork.

If the compiler is saying that the values are undeclared, you should be asking yourself where did you get these values from? Track down where you believe they should be (the Reachable prefix is a clue) and then find your mistake.

It took literally 30 seconds to track this error down. You'll save yourself a great deal of time, by asking yourself obvious questions and listening to the compiler messages.

It would also be cleaner and more efficient to check,

if (internetStatus == NotReachable) {}

Also I wouldn't create an instance variable and save the reachability as this may change (as this is for a mobile device). It is safer to check for a connection every time you need one. As such I would be inclined to write,

if ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == NotReachable) {}
Oliver White
You are 100% right.
Igor Kilimnik