首先引入头文件AFNetworkReachabilityManager.h
然后输入下列代码
AFNetworkReachabilityManager *netStatus = [AFNetworkReachabilityManager sharedManager];
[netStatus startMonitoring];
[netStatus setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
if (status == AFNetworkReachabilityStatusNotReachable) {
[self tishiWithTitle:@"提示" message:@"当前未连接网络" title2:@"返回"];
}
if (status == AFNetworkReachabilityStatusReachableViaWWAN) {
[self tishiWithTitle:@"提示" message:@"当前网络为WWAN" title2:@"返回"];
}
if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
[self tishiWithTitle:@"提示" message:@"当前网络为WiFi" title2:@"返回"];
}
if (status == AFNetworkReachabilityStatusUnknown) {
[self tishiWithTitle:@"提示" message:@"当前为未知网" title2:@"返回"];
}
}];
- (void)tishiWithTitle:(NSString *)title message:(NSString *)message title2:(NSString *)title2
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:title2 style:UIAlertActionStyleDefault handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:^{
}];
}
网友评论