RAReachability.m 3.6 KB

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