引入 #import "AFNetworkReachabilityManager.h"
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
// 提示:要监控网络连接状态,必须要先调用单例的startMonitoring方法
[manager startMonitoring];
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
if (status == -1) {
NSLog(@"未识别网络");
UIAlertController *alertview=[UIAlertController alertControllerWithTitle:@"未识别网络"
message:@"请打开设置→网络/Wi-Fi"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel=[UIAlertAction actionWithTitle:@"取消"
style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *defult = [UIAlertAction actionWithTitle:@"确定"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
}];
[alertview addAction:cancel];
[alertview addAction:defult];
[self presentViewController:alertview animated:YES completion:nil];
}
if (status == 0) {
NSLog(@"未连接网络");
UIAlertController *alertview=[UIAlertController alertControllerWithTitle:@"未连接网络"
message:@"请打开设置-蜂窝移动网络/Wi-Fi"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *defult = [UIAlertAction actionWithTitle:@"确定"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
}];
[alertview addAction:defult];
[self presentViewController:alertview animated:YES completion:nil];
}
if (status == 1) {
NSLog(@"3G/4G网络");
}
if (status == 2) {
NSLog(@"Wifi网络");
}
}];