天天省钱快报

NetworkTool.m 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // NetworkTool.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/6/13.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "NetworkTool.h"
  9. #import <CoreTelephony/CTCellularData.h>
  10. @implementation NetworkTool
  11. /*
  12. CTCellularData在iOS9之前是私有类,权限设置是iOS10开始的,所以App Store审核没有问题
  13. 获取网络权限状态
  14. */
  15. + (void)networkStatus:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  16. //2.根据权限执行相应的交互
  17. if (@available(iOS 9.0, *)) {
  18. CTCellularData *cellularData = [[CTCellularData alloc] init];
  19. /*
  20. 此函数会在网络权限改变时再次调用
  21. */
  22. cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state) {
  23. switch (state) {
  24. case kCTCellularDataRestricted:
  25. {
  26. NSLog(@"Restricted");
  27. //2.1权限关闭的情况下 再次请求网络数据会弹出设置网络提示
  28. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"检测到您没有启用网络,点击设置按钮进行设置" preferredStyle:UIAlertControllerStyleAlert];
  29. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
  30. UIAlertAction *set = [UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  31. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  32. if([[UIApplication sharedApplication]canOpenURL:url] ) {
  33. if (@available(iOS 10.0, *)) {
  34. [[UIApplication sharedApplication] openURL:url options:@{}completionHandler:^(BOOL success) {
  35. }];
  36. } else {
  37. [[UIApplication sharedApplication]openURL:url];
  38. }
  39. }
  40. }];
  41. [alert addAction:cancel];
  42. [alert addAction:set];
  43. [[self currentViewController] presentViewController:alert animated:YES completion:nil];
  44. break;
  45. }
  46. case kCTCellularDataNotRestricted:
  47. NSLog(@"NotRestricted");
  48. //2.2已经开启网络权限 监听网络状态
  49. [self addReachabilityManager:application didFinishLaunchingWithOptions:launchOptions];
  50. // [self getInfo_application:application didFinishLaunchingWithOptions:launchOptions];
  51. break;
  52. case kCTCellularDataRestrictedStateUnknown:
  53. NSLog(@"Unknown");
  54. //2.3未知情况 (还没有遇到推测是有网络但是连接不正常的情况下)
  55. break;
  56. default:
  57. break;
  58. }
  59. };
  60. } else {
  61. // Fallback on earlier versions
  62. }
  63. }
  64. /**
  65. 实时检查当前网络状态
  66. */
  67. + (void)addReachabilityManager:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  68. AFNetworkReachabilityManager *afNetworkReachabilityManager = [AFNetworkReachabilityManager sharedManager];
  69. //这个可以放在需要侦听的页面
  70. // [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(afNetworkStatusChanged:) name:AFNetworkingReachabilityDidChangeNotification object:nil];
  71. [afNetworkReachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
  72. switch (status) {
  73. case AFNetworkReachabilityStatusNotReachable:{
  74. NSLog(@"网络不通:%@",@(status) );
  75. break;
  76. }
  77. case AFNetworkReachabilityStatusReachableViaWiFi:{
  78. NSLog(@"网络通过WIFI连接:%@",@(status));
  79. break;
  80. }
  81. case AFNetworkReachabilityStatusReachableViaWWAN:{
  82. NSLog(@"网络通过无线连接:%@",@(status) );
  83. break;
  84. }
  85. default:
  86. break;
  87. }
  88. }];
  89. [afNetworkReachabilityManager startMonitoring]; //开启网络监视器;
  90. }
  91. + (int)checkNetWorkPermission
  92. {
  93. if (@available(iOS 9.0, *)) {
  94. CTCellularData *cellularData = [[CTCellularData alloc]init];
  95. CTCellularDataRestrictedState state = cellularData.restrictedState;
  96. return state;
  97. } else {
  98. return -1;
  99. }
  100. }
  101. + (UIViewController *)currentViewController{
  102. UIViewController * currVC = nil;
  103. UIWindow *window = [UIApplication sharedApplication].delegate.window;
  104. UIViewController * Rootvc = window.rootViewController;
  105. do {
  106. if ([Rootvc isKindOfClass:[UINavigationController class]]) {
  107. UINavigationController * nav = (UINavigationController *)Rootvc;
  108. UIViewController * v = [nav.viewControllers lastObject];
  109. currVC = v;
  110. Rootvc = v.presentedViewController;
  111. continue;
  112. }else if([Rootvc isKindOfClass:[UITabBarController class]]){
  113. UITabBarController * tabVC = (UITabBarController *)Rootvc;
  114. currVC = tabVC;
  115. Rootvc = [tabVC.viewControllers objectAtIndex:tabVC.selectedIndex];
  116. continue;
  117. }
  118. } while (Rootvc!=nil);
  119. return currVC;
  120. }
  121. @end