转自 http://hi.baidu.com/feng20068123/item/275eb5c2d9bf0a68f6c95d63
Apple 的 例程 Reachability 中介绍了取得/检测网络状态的方法。在你的程序中使用 Reachability 只须将该例程中的 Reachability.h 和 Reachability.m 拷贝到你的工程中。如下图:

然后将 SystemConfiguration.framework 添加进工程:

Reachability 中定义了3种网络状态。
// the network state of the device for Reachability 1.5.
typedef enum{
NotReachable= 0,
ReachableViaCarrierDataNetwork,
ReachableViaWiFiNetwork
} NetworkStatus;
// the network state of the device for Reachability 2.0.
typedefenum{
NotReachable= 0,
ReachableViaWiFi,
ReachableViaWWAN
} NetworkStatus;
- 
NotReachable
无连接
 - 
ReachableViaCarrierDataNetwork (ReachableViaWWAN)
使用3G/GPRS网络
 - 
ReachableViaWiFiNetwork (ReachableViaWiFi)
使用WiFi网络
 
比如检测某一特定站点的接续状况,可以使用下面的代码:
Reachability*r= [ReachabilityreachabilityWithHostName:@“www.apple.com”];
switch([r currentReachabilityStatus])
{ caseNotReachable:
// 没有网络连接
break;
caseReachableViaWWAN:
// 使用3G网络break; caseReachableViaWiFi: // 使用WiFi网络break;}
检测当前网络环境
程序启动时,如果想检测可用的网络环境,可以像这样。
// 是否wifi
+ (BOOL) IsEnableWIFI{
return([[ReachabilityreachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}
// 是否3G
+ (BOOL) IsEnable3G{
return([[ReachabilityreachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
}
                    
                
                
            
        
浙公网安备 33010602011771号