GULReachabilityChecker.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Copyright 2017 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <Foundation/Foundation.h>
  15. #import "GULReachabilityChecker+Internal.h"
  16. #import "Private/GULReachabilityChecker.h"
  17. #import "Private/GULReachabilityMessageCode.h"
  18. #import <GoogleUtilities/GULLogger.h>
  19. #import <GoogleUtilities/GULReachabilityChecker.h>
  20. static GULLoggerService kGULLoggerReachability = @"[GULReachability]";
  21. static void ReachabilityCallback(SCNetworkReachabilityRef reachability,
  22. SCNetworkReachabilityFlags flags,
  23. void *info);
  24. static const struct GULReachabilityApi kGULDefaultReachabilityApi = {
  25. SCNetworkReachabilityCreateWithName,
  26. SCNetworkReachabilitySetCallback,
  27. SCNetworkReachabilityScheduleWithRunLoop,
  28. SCNetworkReachabilityUnscheduleFromRunLoop,
  29. CFRelease,
  30. };
  31. static NSString *const kGULReachabilityUnknownStatus = @"Unknown";
  32. static NSString *const kGULReachabilityConnectedStatus = @"Connected";
  33. static NSString *const kGULReachabilityDisconnectedStatus = @"Disconnected";
  34. @interface GULReachabilityChecker ()
  35. @property(nonatomic, assign) const struct GULReachabilityApi *reachabilityApi;
  36. @property(nonatomic, assign) GULReachabilityStatus reachabilityStatus;
  37. @property(nonatomic, copy) NSString *host;
  38. @property(nonatomic, assign) SCNetworkReachabilityRef reachability;
  39. @end
  40. @implementation GULReachabilityChecker
  41. @synthesize reachabilityApi = reachabilityApi_;
  42. @synthesize reachability = reachability_;
  43. - (const struct GULReachabilityApi *)reachabilityApi {
  44. return reachabilityApi_;
  45. }
  46. - (void)setReachabilityApi:(const struct GULReachabilityApi *)reachabilityApi {
  47. if (reachability_) {
  48. GULLogError(kGULLoggerReachability, NO,
  49. [NSString stringWithFormat:@"I-REA%06ld", (long)kGULReachabilityMessageCode000],
  50. @"Cannot change reachability API while reachability is running. "
  51. @"Call stop first.");
  52. return;
  53. }
  54. reachabilityApi_ = reachabilityApi;
  55. }
  56. @synthesize reachabilityStatus = reachabilityStatus_;
  57. @synthesize host = host_;
  58. @synthesize reachabilityDelegate = reachabilityDelegate_;
  59. - (BOOL)isActive {
  60. return reachability_ != nil;
  61. }
  62. - (void)setReachabilityDelegate:(id<GULReachabilityDelegate>)reachabilityDelegate {
  63. if (reachabilityDelegate &&
  64. (![(NSObject *)reachabilityDelegate conformsToProtocol:@protocol(GULReachabilityDelegate)])) {
  65. GULLogError(kGULLoggerReachability, NO,
  66. [NSString stringWithFormat:@"I-NET%06ld", (long)kGULReachabilityMessageCode005],
  67. @"Reachability delegate doesn't conform to Reachability protocol.");
  68. return;
  69. }
  70. reachabilityDelegate_ = reachabilityDelegate;
  71. }
  72. - (instancetype)initWithReachabilityDelegate:(id<GULReachabilityDelegate>)reachabilityDelegate
  73. withHost:(NSString *)host {
  74. self = [super init];
  75. if (!host || !host.length) {
  76. GULLogError(kGULLoggerReachability, NO,
  77. [NSString stringWithFormat:@"I-REA%06ld", (long)kGULReachabilityMessageCode001],
  78. @"Invalid host specified");
  79. return nil;
  80. }
  81. if (self) {
  82. [self setReachabilityDelegate:reachabilityDelegate];
  83. reachabilityApi_ = &kGULDefaultReachabilityApi;
  84. reachabilityStatus_ = kGULReachabilityUnknown;
  85. host_ = [host copy];
  86. reachability_ = nil;
  87. }
  88. return self;
  89. }
  90. - (void)dealloc {
  91. reachabilityDelegate_ = nil;
  92. [self stop];
  93. }
  94. - (BOOL)start {
  95. if (!reachability_) {
  96. reachability_ = reachabilityApi_->createWithNameFn(kCFAllocatorDefault, [host_ UTF8String]);
  97. if (!reachability_) {
  98. return NO;
  99. }
  100. SCNetworkReachabilityContext context = {
  101. 0, /* version */
  102. (__bridge void *)(self), /* info (passed as last parameter to reachability callback) */
  103. NULL, /* retain */
  104. NULL, /* release */
  105. NULL /* copyDescription */
  106. };
  107. if (!reachabilityApi_->setCallbackFn(reachability_, ReachabilityCallback, &context) ||
  108. !reachabilityApi_->scheduleWithRunLoopFn(reachability_, CFRunLoopGetMain(),
  109. kCFRunLoopCommonModes)) {
  110. reachabilityApi_->releaseFn(reachability_);
  111. reachability_ = nil;
  112. GULLogError(kGULLoggerReachability, NO,
  113. [NSString stringWithFormat:@"I-REA%06ld", (long)kGULReachabilityMessageCode002],
  114. @"Failed to start reachability handle");
  115. return NO;
  116. }
  117. }
  118. GULLogDebug(kGULLoggerReachability, NO,
  119. [NSString stringWithFormat:@"I-REA%06ld", (long)kGULReachabilityMessageCode003],
  120. @"Monitoring the network status");
  121. return YES;
  122. }
  123. - (void)stop {
  124. if (reachability_) {
  125. reachabilityStatus_ = kGULReachabilityUnknown;
  126. reachabilityApi_->unscheduleFromRunLoopFn(reachability_, CFRunLoopGetMain(),
  127. kCFRunLoopCommonModes);
  128. reachabilityApi_->releaseFn(reachability_);
  129. reachability_ = nil;
  130. }
  131. }
  132. - (GULReachabilityStatus)statusForFlags:(SCNetworkReachabilityFlags)flags {
  133. GULReachabilityStatus status = kGULReachabilityNotReachable;
  134. // If the Reachable flag is not set, we definitely don't have connectivity.
  135. if (flags & kSCNetworkReachabilityFlagsReachable) {
  136. // Reachable flag is set. Check further flags.
  137. if (!(flags & kSCNetworkReachabilityFlagsConnectionRequired)) {
  138. // Connection required flag is not set, so we have connectivity.
  139. #if TARGET_OS_IOS || TARGET_OS_TV
  140. status = (flags & kSCNetworkReachabilityFlagsIsWWAN) ? kGULReachabilityViaCellular
  141. : kGULReachabilityViaWifi;
  142. #elif TARGET_OS_OSX
  143. status = kGULReachabilityViaWifi;
  144. #endif
  145. } else if ((flags & (kSCNetworkReachabilityFlagsConnectionOnDemand |
  146. kSCNetworkReachabilityFlagsConnectionOnTraffic)) &&
  147. !(flags & kSCNetworkReachabilityFlagsInterventionRequired)) {
  148. // If the connection on demand or connection on traffic flag is set, and user intervention
  149. // is not required, we have connectivity.
  150. #if TARGET_OS_IOS || TARGET_OS_TV
  151. status = (flags & kSCNetworkReachabilityFlagsIsWWAN) ? kGULReachabilityViaCellular
  152. : kGULReachabilityViaWifi;
  153. #elif TARGET_OS_OSX
  154. status = kGULReachabilityViaWifi;
  155. #endif
  156. }
  157. }
  158. return status;
  159. }
  160. - (void)reachabilityFlagsChanged:(SCNetworkReachabilityFlags)flags {
  161. GULReachabilityStatus status = [self statusForFlags:flags];
  162. if (reachabilityStatus_ != status) {
  163. NSString *reachabilityStatusString;
  164. if (status == kGULReachabilityUnknown) {
  165. reachabilityStatusString = kGULReachabilityUnknownStatus;
  166. } else {
  167. reachabilityStatusString = (status == kGULReachabilityNotReachable)
  168. ? kGULReachabilityDisconnectedStatus
  169. : kGULReachabilityConnectedStatus;
  170. }
  171. GULLogDebug(kGULLoggerReachability, NO,
  172. [NSString stringWithFormat:@"I-REA%06ld", (long)kGULReachabilityMessageCode004],
  173. @"Network status has changed. Code:%@, status:%@", @(status),
  174. reachabilityStatusString);
  175. reachabilityStatus_ = status;
  176. [reachabilityDelegate_ reachability:self statusChanged:reachabilityStatus_];
  177. }
  178. }
  179. @end
  180. static void ReachabilityCallback(SCNetworkReachabilityRef reachability,
  181. SCNetworkReachabilityFlags flags,
  182. void *info) {
  183. GULReachabilityChecker *checker = (__bridge GULReachabilityChecker *)info;
  184. [checker reachabilityFlagsChanged:flags];
  185. }
  186. // This function used to be at the top of the file, but it was moved here
  187. // as a workaround for a suspected compiler bug. When compiled in Release mode
  188. // and run on an iOS device with WiFi disabled, the reachability code crashed
  189. // when calling SCNetworkReachabilityScheduleWithRunLoop, or shortly thereafter.
  190. // After unsuccessfully trying to diagnose the cause of the crash, it was
  191. // discovered that moving this function to the end of the file magically fixed
  192. // the crash. If you are going to edit this file, exercise caution and make sure
  193. // to test thoroughly with an iOS device under various network conditions.
  194. const NSString *GULReachabilityStatusString(GULReachabilityStatus status) {
  195. switch (status) {
  196. case kGULReachabilityUnknown:
  197. return @"Reachability Unknown";
  198. case kGULReachabilityNotReachable:
  199. return @"Not reachable";
  200. case kGULReachabilityViaWifi:
  201. return @"Reachable via Wifi";
  202. case kGULReachabilityViaCellular:
  203. return @"Reachable via Cellular Data";
  204. default:
  205. return [NSString stringWithFormat:@"Invalid reachability status %d", (int)status];
  206. }
  207. }