苹果官方提供 Reachability 检测网络状态
https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
添加框架SystemConfiguration.framework
2 3 - (void)viewDidLoad 4 { 5 [super viewDidLoad]; 6 7 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil];}
1 - (void)dealloc 2 { 3 4 [[NSNotificationCenter defaultCenter] removeObserver:self]; 5 }
1 - (void)checkNetworkState 2 { 3 // 1.检测wifi状态 4 Reachability *wifi = [Reachability reachabilityForLocalWiFi]; 5 6 // 2.检测网络(WIFI3G2.5G) 7 Reachability *conn = [Reachability reachabilityForInternetConnection]; 8 9 // 3.判断网络状态 10 if ([wifi currentReachabilityStatus] != NotReachable) { // wifi 11 12 13 } else if ([conn currentReachabilityStatus] != NotReachable) { // 手机网络 14 15 16 } else { // 没有网络 17 18 19 } 20 }