Reachability sample code

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-14 06:11:08

问题


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))
{
}

回答1:


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) {}


来源:https://stackoverflow.com/questions/1942968/reachability-sample-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!