1. pch文件 / Podfile
#import <AFNetworking.h>
#import <MBProgressHUD.h>
pod 'AFNetworking', '4.0.1'
pod 'MBProgressHUD', '~> 1.0.0'
2.Appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
........
[CommonTool startMonitoringForNetWork];
.......
3.Common1.h
+(void)startMonitoringForNetWork;
+(BOOL)judgeNetWorkStatusWithoutAlert;
+(BOOL)judgeNetWorkStatus;
+(void)showAlert:(NSString *)title message:(NSString *)message button1:(NSString *)bt1 button2:(NSString *)bt2;
+ (void)showAlert:(NSString *)title message:(NSString *)message button1:(NSString *)bt1 button2:(NSString *)bt2 handler1:(void (^ __nullable)(UIAlertAction *action))handler1 handler2:(void (^ __nullable)(UIAlertAction *action))handler2;
4.Common2.m
#import <NetworkExtension/NetworkExtension.h>
// **************** 网络状态检测 start ***************************************
+(void)startMonitoringForNetWork
{
AFNetworkReachabilityManager *afNetworkReachabilityManager = [AFNetworkReachabilityManager sharedManager];
[afNetworkReachabilityManager startMonitoring]; //开启网络监视器;
[afNetworkReachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusNotReachable:{
NSLog(@"网络不通" );
break;
}
case AFNetworkReachabilityStatusReachableViaWiFi:{
NSLog(@"网络通过WIFI连接");
// [SQLiteDemo uploadSavedCbbt];
break;
}
case AFNetworkReachabilityStatusReachableViaWWAN:{
// [SQLiteDemo uploadSavedCbbt];
NSLog(@"网络通过蜂窝连接");
break;
}
default:
break;
}
}];
}
+(BOOL)judgeNetWorkStatusWithoutAlert
{
if (![AFNetworkReachabilityManager sharedManager].isReachable) {
return false;
}
return true;
}
+(BOOL)judgeNetWorkStatus
{
if (![AFNetworkReachabilityManager sharedManager].isReachable) {
[self dismissLoading];
if ([CommonTool.presentingVC isKindOfClass:[UIAlertController class]]) {
UIAlertController *alertx = (UIAlertController *)CommonTool.presentingVC;
if ([alertx.title isEqualToString:@"No Internet Connection"]) {
return false;
}
}
[CommonTool showAlert:@"No Internet Connection" message:@"Make sure your device is connected to the internet" button1:@"OK" button2: nil];
return false;
}
return true;
}
+(void)showAlert:(NSString *)title message:(NSString *)message button1:(NSString *)bt1 button2:(NSString *)bt2
{
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
if (bt1) {
[alertVC addAction:[UIAlertAction actionWithTitle:bt1 style:UIAlertActionStyleCancel handler:nil]];
}
if (bt2) {
[alertVC addAction:[UIAlertAction actionWithTitle:bt2 style:UIAlertActionStyleCancel handler:nil]];
}
[CommonTool.presentingVC presentViewController:alertVC animated:YES completion:nil];
}
+ (void)showAlert:(NSString *)title message:(NSString *)message button1:(NSString *)bt1 button2:(NSString *)bt2 handler1:(void (^ __nullable)(UIAlertAction *action))handler1 handler2:(void (^ __nullable)(UIAlertAction *action))handler2 {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
if (bt1) {
[alertVC addAction:[UIAlertAction actionWithTitle:bt1 style:UIAlertActionStyleDefault handler:handler1]];
}
if (bt2) {
[alertVC addAction:[UIAlertAction actionWithTitle:bt2 style:UIAlertActionStyleDefault handler:handler2]];
}
[CommonTool.presentingVC presentViewController:alertVC animated:YES completion:nil];
}
/**
Description:
@return Currently PresentingViewController
*/
+ (UIViewController *)presentingVC{
UIWindow * window = [CommonTool getAppDelegateWindow];
if (window.windowLevel != UIWindowLevelNormal){
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow * tmpWin in windows){
if (tmpWin.windowLevel == UIWindowLevelNormal){
window = tmpWin;
break;
}
}
}
UIViewController *rootViewController = window.rootViewController;
while (rootViewController.presentedViewController) {
rootViewController = rootViewController.presentedViewController;
}
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabBarController = (UITabBarController *)rootViewController;
UIViewController *selectedVC=tabBarController.selectedViewController;
if ([selectedVC isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)selectedVC;
return navigationController.visibleViewController;
}else{
return selectedVC;
}
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return navigationController.visibleViewController;
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return presentedViewController;
} else {
return rootViewController;
}
}
+ (UIWindow*)getAppDelegateWindow {
return [[[UIApplication sharedApplication] delegate] window];
}
+ (void)dismissLoading{
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:[CommonTool getAppDelegateWindow] animated:YES];
}) ;
}
// **************** 网络状态检测 End*******************************************
5. 调用
if(![CommonTool judgeNetWorkStatus]){
// 无网络
return;
}
网友评论