RAReachability.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // RAReachability.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/10/23.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RAReachability.h"
  9. #import "Reachability.h"
  10. #define host @"https://ra.apexshipping.com"
  11. @interface RAReachability ()
  12. @property (nonatomic,copy) void(^ReachabilityBlk)(BOOL);
  13. @end
  14. @implementation RAReachability {
  15. Reachability *_hostReachability; ///< 服务器可达
  16. Reachability *_connectionReachability; ///< 本机网络状态
  17. }
  18. + (instancetype)defaultReachability {
  19. static RAReachability *reachability;
  20. static dispatch_once_t token;
  21. dispatch_once(&token, ^{
  22. reachability = [[RAReachability alloc] _init];
  23. });
  24. return reachability;
  25. }
  26. - (instancetype)_init {
  27. if (self = [super init]) {
  28. self->_hostReachability = [Reachability reachabilityWithHostName:host];
  29. self->_connectionReachability = [Reachability reachabilityForInternetConnection];
  30. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityConnectionChanged:) name:kReachabilityChangedNotification object:nil];
  31. }
  32. return self;
  33. }
  34. - (void)dealloc {
  35. [[NSNotificationCenter defaultCenter] removeObserver:self];
  36. }
  37. - (void)startNotifier:(void(^)(BOOL reachable))reachableBlk {
  38. self.ReachabilityBlk = reachableBlk;
  39. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  40. [self->_hostReachability startNotifier];
  41. [self->_connectionReachability startNotifier];
  42. [[NSRunLoop currentRunLoop] run];
  43. });
  44. }
  45. - (void)stopNotifier {
  46. self.ReachabilityBlk = nil;
  47. [self->_hostReachability stopNotifier];
  48. [self->_connectionReachability stopNotifier];
  49. }
  50. - (void)reachabilityConnectionChanged:(NSNotification *)notification {
  51. Reachability *reachability = [notification object];
  52. if ([reachability isMemberOfClass:[Reachability class]]) {
  53. NetworkStatus status = reachability.currentReachabilityStatus;
  54. dispatch_async(dispatch_get_main_queue(), ^{
  55. if (reachability == self->_hostReachability) {
  56. switch (status) {
  57. case NotReachable: {
  58. if (self.ReachabilityBlk) {
  59. self.ReachabilityBlk(false);
  60. }
  61. }
  62. break;
  63. case ReachableViaWiFi: {
  64. if (self.ReachabilityBlk) {
  65. self.ReachabilityBlk(true);
  66. }
  67. }
  68. break;
  69. case ReachableViaWWAN: {
  70. if (self.ReachabilityBlk) {
  71. self.ReachabilityBlk(true);
  72. }
  73. }
  74. break;
  75. default:
  76. break;
  77. }
  78. }
  79. else if (reachability == self->_connectionReachability) {
  80. switch (status) {
  81. case NotReachable: {
  82. if (self.ReachabilityBlk) {
  83. self.ReachabilityBlk(false);
  84. }
  85. }
  86. break;
  87. default: {
  88. if (self.ReachabilityBlk) {
  89. self.ReachabilityBlk(true);
  90. }
  91. }
  92. break;
  93. }
  94. }
  95. });
  96. }
  97. }
  98. @end