GULReachabilityChecker.m 9.6 KB

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