一.AFN
(void)afnReachability{
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
// AFNetworkReachabilityStatusUnknown = -1,
// AFNetworkReachabilityStatusNotReachable = 0,
// AFNetworkReachabilityStatusReachableViaWWAN = 1,
// AFNetworkReachabilityStatusReachableViaWiFi = 2,
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"WIFI");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"移动蜂窝");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"无网络");
break;
case AFNetworkReachabilityStatusUnknown:
NSLog(@"未知");
break;
default:
NSLog(@"未知");
break;
}
}];
[manager startMonitoring];
}
2.Reachability
@property (nonatomic, strong) Reachability *r;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachability) name:@"kReachabilityChangedNotification" object:nil];
self.r = [Reachability reachabilityForLocalWiFi];
[self.r startNotifier];
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)reachability{
// NotReachable = 0,
// ReachableViaWiFi = 2,
// ReachableViaWWAN = 1
if ([Reachability reachabilityForInternetConnection].currentReachabilityStatus == ReachableViaWWAN) {
NSLog(@"移动蜂窝");
}else if ([Reachability reachabilityForInternetConnection].currentReachabilityStatus == ReachableViaWiFi){
NSLog(@"WIFI");
}else if ([Reachability reachabilityForInternetConnection].currentReachabilityStatus == NotReachable){
NSLog(@"无网络");
}else{
NSLog(@"未知");
}
}