GULReachabilityChecker.m 9.5 KB

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