ThirdPartService.m 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // ThirdPartService.m
  3. // westkissMob
  4. //
  5. // Created by 王猛 on 2022/9/5.
  6. //
  7. #import "ThirdPartService.h"
  8. #import <IQKeyboardManager/IQKeyboardManager.h>
  9. // 检测网络状态
  10. #import <Reachability/Reachability.h>
  11. @interface ThirdPartService()
  12. @property (strong, nonatomic) Reachability *internetReachability;
  13. @end
  14. @implementation ThirdPartService
  15. + (instancetype)sharedInstance {
  16. static id sharedInstance = nil;
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. sharedInstance = [[self alloc] init];
  20. });
  21. return sharedInstance;
  22. }
  23. + (void)load{
  24. [[ThirdPartService sharedInstance] setupKeyboardManager];
  25. [[ThirdPartService sharedInstance] setupReachability];
  26. }
  27. #pragma mark - 设置全局键盘
  28. - (void)setupKeyboardManager {
  29. // 设置键盘监听管理
  30. IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
  31. manager.toolbarDoneBarButtonItemText =@"Done";
  32. manager.enable=YES;
  33. manager.toolbarManageBehaviour =IQAutoToolbarByTag;
  34. manager.shouldResignOnTouchOutside=NO;
  35. manager.shouldToolbarUsesTextFieldTintColor=NO;
  36. manager.enableAutoToolbar=YES;
  37. manager.shouldShowToolbarPlaceholder = NO;
  38. manager.shouldResignOnTouchOutside = YES; // 控制点击背景是否收起键盘
  39. }
  40. #pragma mark - 检测网络状态
  41. - (void)setupReachability {
  42. //添加一个系统通知
  43. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
  44. //初始化
  45. self.internetReachability=[Reachability reachabilityForInternetConnection];
  46. //通知添加到Run Loop
  47. [self.internetReachability startNotifier];
  48. [self updateInterfaceWithReachability:_internetReachability];
  49. }
  50. - (void)reachabilityChanged:(NSNotification *)note {
  51. Reachability* curReach = [note object];
  52. if ([curReach isKindOfClass:[Reachability class]]) {
  53. NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
  54. [self updateInterfaceWithReachability: curReach];
  55. } else {
  56. UIViewController *topVc = topViewController();
  57. [topVc.view makeToast:@"Network status has been switched."];
  58. }
  59. }
  60. - (void)updateInterfaceWithReachability:(Reachability *)reachability {
  61. NetworkStatus netStatus = [reachability currentReachabilityStatus];
  62. UIViewController *topVc = topViewController();
  63. [NSNotificationCenter.defaultCenter postNotificationName:netStatusUpdate object:nil];
  64. switch (netStatus) {
  65. case NotReachable:
  66. NSLog(@"====当前网络状态不可达=======");
  67. [topVc.view makeToast:@"Current network status is unavailable"];
  68. break;
  69. case ReachableViaWiFi:
  70. NSLog(@"====当前网络状态为Wifi=======");
  71. // [topVc.view makeToast:@"The current network status is WiFi."];
  72. break;
  73. case ReachableViaWWAN:
  74. NSLog(@"====当前网络状态为蜂窝网络=======");
  75. // [topVc.view makeToast:@"The current status is cellular network."];
  76. break;
  77. }
  78. }
  79. @end