用AFN检测当前网络状态 2018.9.29
先导入AFNetworking第三方文件
再导入 #import “AFNetworkReachabilityManager.h”
这里是用AFNetworking里的Reachability进行网络检测
也可以用Reachability做网络检测
下面是具体实现
#import “ViewController.h”
#import “AFNetworkReachabilityManager.h”
@interface ViewController ()
@end
@implementation ViewController
-
(void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0)green:((float)arc4random_uniform(256) / 255.0)blue:((float)arc4random_uniform(256) / 255.0)alpha:1.0];
[self reachability];
}
-
(void)reachability {
// 1.获得网络监控的管理者
AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
// 2.设置网络状态改变后的处理
[mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
// 当网络状态改变了, 就会调用这个block if (status == AFNetworkReachabilityStatusReachableViaWiFi) { NSLog(@"已连接Wi-Fi网络"); UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"已连接Wi-Fi网络" message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDestructive handler:nil]; [alertController addAction:cancelAction]; [alertController addAction:okAction]; [self presentViewController:alertController animated:YES completion:nil]; }else if (status == AFNetworkReachabilityStatusReachableViaWWAN){ NSLog(@"已连接蜂窝移动网络"); UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"已连接蜂窝移动网络" message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDestructive handler:nil]; [alertController addAction:cancelAction]; [alertController addAction:okAction]; [self presentViewController:alertController animated:YES completion:nil]; }else if (status == AFNetworkReachabilityStatusUnknown){ NSLog(@"已连接未知网络"); UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"已连接未知网络" message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDestructive handler:nil]; [alertController addAction:cancelAction]; [alertController addAction:okAction]; [self presentViewController:alertController animated:YES completion:nil]; }else if (status == AFNetworkReachabilityStatusNotReachable){ NSLog(@"没有网络(断网)"); UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"没有网络(断网)" message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDestructive handler:nil]; [alertController addAction:cancelAction]; [alertController addAction:okAction]; [self presentViewController:alertController animated:YES completion:nil]; }
}];
// 3.开始监控
[mgr startMonitoring];
}