123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- //
- // NetworkTool.m
- // YouHuiProject
- //
- // Created by 小花 on 2018/6/13.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "NetworkTool.h"
- #import <CoreTelephony/CTCellularData.h>
- @implementation NetworkTool
- /*
- CTCellularData在iOS9之前是私有类,权限设置是iOS10开始的,所以App Store审核没有问题
- 获取网络权限状态
- */
- + (void)networkStatus:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- //2.根据权限执行相应的交互
- if (@available(iOS 9.0, *)) {
-
- CTCellularData *cellularData = [[CTCellularData alloc] init];
- /*
- 此函数会在网络权限改变时再次调用
- */
- cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state) {
- switch (state) {
- case kCTCellularDataRestricted:
- {
-
- NSLog(@"Restricted");
- //2.1权限关闭的情况下 再次请求网络数据会弹出设置网络提示
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"检测到您没有启用网络,点击设置按钮进行设置" preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
- UIAlertAction *set = [UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
- NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
- if([[UIApplication sharedApplication]canOpenURL:url] ) {
- if (@available(iOS 10.0, *)) {
- [[UIApplication sharedApplication] openURL:url options:@{}completionHandler:^(BOOL success) {
- }];
- } else {
- [[UIApplication sharedApplication]openURL:url];
- }
- }
- }];
-
- [alert addAction:cancel];
- [alert addAction:set];
- [[self currentViewController] presentViewController:alert animated:YES completion:nil];
-
- break;
- }
- case kCTCellularDataNotRestricted:
-
- NSLog(@"NotRestricted");
- //2.2已经开启网络权限 监听网络状态
- [self addReachabilityManager:application didFinishLaunchingWithOptions:launchOptions];
- // [self getInfo_application:application didFinishLaunchingWithOptions:launchOptions];
- break;
- case kCTCellularDataRestrictedStateUnknown:
-
- NSLog(@"Unknown");
- //2.3未知情况 (还没有遇到推测是有网络但是连接不正常的情况下)
- break;
-
- default:
- break;
- }
- };
-
- } else {
- // Fallback on earlier versions
- }
-
- }
- /**
- 实时检查当前网络状态
- */
- + (void)addReachabilityManager:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- AFNetworkReachabilityManager *afNetworkReachabilityManager = [AFNetworkReachabilityManager sharedManager];
-
- //这个可以放在需要侦听的页面
- // [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(afNetworkStatusChanged:) name:AFNetworkingReachabilityDidChangeNotification object:nil];
- [afNetworkReachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
- switch (status) {
- case AFNetworkReachabilityStatusNotReachable:{
- NSLog(@"网络不通:%@",@(status) );
- break;
- }
- case AFNetworkReachabilityStatusReachableViaWiFi:{
- NSLog(@"网络通过WIFI连接:%@",@(status));
-
- break;
- }
- case AFNetworkReachabilityStatusReachableViaWWAN:{
- NSLog(@"网络通过无线连接:%@",@(status) );
-
- break;
- }
- default:
- break;
- }
- }];
-
- [afNetworkReachabilityManager startMonitoring]; //开启网络监视器;
- }
- + (int)checkNetWorkPermission
- {
- if (@available(iOS 9.0, *)) {
- CTCellularData *cellularData = [[CTCellularData alloc]init];
- CTCellularDataRestrictedState state = cellularData.restrictedState;
- return state;
- } else {
- return -1;
- }
-
- }
- + (UIViewController *)currentViewController{
-
- UIViewController * currVC = nil;
- UIWindow *window = [UIApplication sharedApplication].delegate.window;
- UIViewController * Rootvc = window.rootViewController;
-
- do {
- if ([Rootvc isKindOfClass:[UINavigationController class]]) {
- UINavigationController * nav = (UINavigationController *)Rootvc;
- UIViewController * v = [nav.viewControllers lastObject];
- currVC = v;
- Rootvc = v.presentedViewController;
- continue;
- }else if([Rootvc isKindOfClass:[UITabBarController class]]){
- UITabBarController * tabVC = (UITabBarController *)Rootvc;
- currVC = tabVC;
- Rootvc = [tabVC.viewControllers objectAtIndex:tabVC.selectedIndex];
- continue;
- }
- } while (Rootvc!=nil);
-
- return currVC;
- }
- @end
|