联系人:石虎QQ: 1224614774 昵称:嗡嘛呢叭咪哄
1.AFN监控联网状态
联网状态status
// 2.设置监听示例代码
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];// 提示:要监控网络连接状态,必须要先调用单例的startMonitoring方法[manager startMonitoring];[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {NSLog(@"%d", status);
}];
2.Reachability监控联网状态苹果提供,导入这两个类使用
// 1.检测wifi状态 Reachability *wifi = [Reachability reachabilityForLocalWiFi];// 2.检测手机是否能上网络(WIFI\3G\2.5G) Reachability *conn = [Reachability reachabilityForInternetConnection];// 3.判断网络状态if ([wifi currentReachabilityStatus] != NotReachable) {// 有wifiNSLog(@"有wifi"); }elseif ([conn currentReachabilityStatus] != NotReachable) {// 没有使用wifi, 使用手机自带网络进行上网NSLog(@"使用手机自带网络进行上网"); }else {// 没有网络NSLog(@"没有网络");
}
①判断联网状态
// 用WIFI// [wifi currentReachabilityStatus] != NotReachable// [conn currentReachabilityStatus] != NotReachable// 没有用WIFI, 只用了手机网络// [wifi currentReachabilityStatus] == NotReachable// [conn currentReachabilityStatus] != NotReachable// 没有网络// [wifi currentReachabilityStatus] == NotReachable// [conn currentReachabilityStatus] == NotReachable
②实时监听网络状态
#import"ViewController.h"#import"Reachability.h"@interfaceHMViewController ()@property (nonatomic,strong) Reachability *conn;@end@implementationHMViewController- (void)viewDidLoad{ [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkState) name:kReachabilityChangedNotification object:nil];self.conn = [Reachability reachabilityForInternetConnection]; [self.conn startNotifier];}- (void)dealloc{ [self.conn stopNotifier]; [[NSNotificationCenter defaultCenter] removeObserver:self];}- (void)checkNetworkState{// 1.检测wifi状态 Reachability *wifi = [Reachability reachabilityForLocalWiFi];// 2.检测手机是否能上网络(WIFI\3G\2.5G) Reachability *conn = [Reachability reachabilityForInternetConnection];// 3.判断网络状态if ([wifi currentReachabilityStatus] != NotReachable) {// 有wifiNSLog(@"有wifi"); }elseif ([conn currentReachabilityStatus] != NotReachable) {// 没有使用wifi, 使用手机自带网络进行上网NSLog(@"使用手机自带网络进行上网"); }else {// 没有网络NSLog(@"没有网络"); }}@end