Reachability.m 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*
  2. File: Reachability.m
  3. Abstract: Basic demonstration of how to use the SystemConfiguration Reachablity APIs.
  4. Version: 2.0.4ddg
  5. */
  6. /*
  7. Significant additions made by Andrew W. Donoho, August 11, 2009.
  8. This is a derived work of Apple's Reachability v2.0 class.
  9. The below license is the new BSD license with the OSI recommended personalizations.
  10. <http://www.opensource.org/licenses/bsd-license.php>
  11. Extensions Copyright (C) 2009 Donoho Design Group, LLC. All Rights Reserved.
  12. Redistribution and use in source and binary forms, with or without
  13. modification, are permitted provided that the following conditions are
  14. met:
  15. * Redistributions of source code must retain the above copyright notice,
  16. this list of conditions and the following disclaimer.
  17. * Redistributions in binary form must reproduce the above copyright
  18. notice, this list of conditions and the following disclaimer in the
  19. documentation and/or other materials provided with the distribution.
  20. * Neither the name of Andrew W. Donoho nor Donoho Design Group, L.L.C.
  21. may be used to endorse or promote products derived from this software
  22. without specific prior written permission.
  23. THIS SOFTWARE IS PROVIDED BY DONOHO DESIGN GROUP, L.L.C. "AS IS" AND ANY
  24. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  26. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  27. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. /*
  36. Apple's Original License on Reachability v2.0
  37. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
  38. ("Apple") in consideration of your agreement to the following terms, and your
  39. use, installation, modification or redistribution of this Apple software
  40. constitutes acceptance of these terms. If you do not agree with these terms,
  41. please do not use, install, modify or redistribute this Apple software.
  42. In consideration of your agreement to abide by the following terms, and subject
  43. to these terms, Apple grants you a personal, non-exclusive license, under
  44. Apple's copyrights in this original Apple software (the "Apple Software"), to
  45. use, reproduce, modify and redistribute the Apple Software, with or without
  46. modifications, in source and/or binary forms; provided that if you redistribute
  47. the Apple Software in its entirety and without modifications, you must retain
  48. this notice and the following text and disclaimers in all such redistributions
  49. of the Apple Software.
  50. Neither the name, trademarks, service marks or logos of Apple Inc. may be used
  51. to endorse or promote products derived from the Apple Software without specific
  52. prior written permission from Apple. Except as expressly stated in this notice,
  53. no other rights or licenses, express or implied, are granted by Apple herein,
  54. including but not limited to any patent rights that may be infringed by your
  55. derivative works or by other works in which the Apple Software may be
  56. incorporated.
  57. The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
  58. WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  59. WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  60. PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  61. COMBINATION WITH YOUR PRODUCTS.
  62. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  63. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  64. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  65. ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
  66. DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
  67. CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
  68. APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  69. Copyright (C) 2009 Apple Inc. All Rights Reserved.
  70. */
  71. /*
  72. Each reachability object now has a copy of the key used to store it in a dictionary.
  73. This allows each observer to quickly determine if the event is important to them.
  74. */
  75. #import <sys/socket.h>
  76. #import <netinet/in.h>
  77. #import <netinet6/in6.h>
  78. #import <arpa/inet.h>
  79. #import <ifaddrs.h>
  80. #import <netdb.h>
  81. #import <CoreFoundation/CoreFoundation.h>
  82. #import "Reachability.h"
  83. NSString *const kInternetConnection = @"InternetConnection";
  84. NSString *const kLocalWiFiConnection = @"LocalWiFiConnection";
  85. NSString *const kReachabilityChangedNotification = @"NetworkReachabilityChangedNotification";
  86. #define CLASS_DEBUG 1 // Turn on logReachabilityFlags. Must also have a project wide defined DEBUG.
  87. #if (defined DEBUG && defined CLASS_DEBUG)
  88. #define logReachabilityFlags(flags) (logReachabilityFlags_(__PRETTY_FUNCTION__, __LINE__, flags))
  89. static NSString *reachabilityFlags_(SCNetworkReachabilityFlags flags) {
  90. #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 30000) // Apple advises you to use the magic number instead of a symbol.
  91. return [NSString stringWithFormat:@"Reachability Flags: %c%c %c%c%c%c%c%c%c",
  92. (flags & kSCNetworkReachabilityFlagsIsWWAN) ? 'W' : '-',
  93. (flags & kSCNetworkReachabilityFlagsReachable) ? 'R' : '-',
  94. (flags & kSCNetworkReachabilityFlagsConnectionRequired) ? 'c' : '-',
  95. (flags & kSCNetworkReachabilityFlagsTransientConnection) ? 't' : '-',
  96. (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-',
  97. (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) ? 'C' : '-',
  98. (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ? 'D' : '-',
  99. (flags & kSCNetworkReachabilityFlagsIsLocalAddress) ? 'l' : '-',
  100. (flags & kSCNetworkReachabilityFlagsIsDirect) ? 'd' : '-'];
  101. #else
  102. // Compile out the v3.0 features for v2.2.1 deployment.
  103. return [NSString stringWithFormat:@"Reachability Flags: %c%c %c%c%c%c%c%c",
  104. (flags & kSCNetworkReachabilityFlagsIsWWAN) ? 'W' : '-',
  105. (flags & kSCNetworkReachabilityFlagsReachable) ? 'R' : '-',
  106. (flags & kSCNetworkReachabilityFlagsConnectionRequired) ? 'c' : '-',
  107. (flags & kSCNetworkReachabilityFlagsTransientConnection) ? 't' : '-',
  108. (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-',
  109. // v3 kSCNetworkReachabilityFlagsConnectionOnTraffic == v2 kSCNetworkReachabilityFlagsConnectionAutomatic
  110. (flags & kSCNetworkReachabilityFlagsConnectionAutomatic) ? 'C' : '-',
  111. // (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ? 'D' : '-', // No v2 equivalent.
  112. (flags & kSCNetworkReachabilityFlagsIsLocalAddress) ? 'l' : '-',
  113. (flags & kSCNetworkReachabilityFlagsIsDirect) ? 'd' : '-'];
  114. #endif
  115. } // reachabilityFlags_()
  116. static void logReachabilityFlags_(const char *name, int line, SCNetworkReachabilityFlags flags) {
  117. NSLog(@"%s (%d) \n\t%@", name, line, reachabilityFlags_(flags));
  118. } // logReachabilityFlags_()
  119. #define logNetworkStatus(status) (logNetworkStatus_(__PRETTY_FUNCTION__, __LINE__, status))
  120. //static void logNetworkStatus_(const char *name, int line, NetworkStatus status) {
  121. //
  122. // NSString *statusString = nil;
  123. //
  124. // switch (status) {
  125. // case kNotReachable:
  126. // statusString = @"Not Reachable";
  127. // break;
  128. // case kReachableViaWWAN:
  129. // statusString = @"Reachable via WWAN";
  130. // break;
  131. // case kReachableViaWiFi:
  132. // statusString = @"Reachable via WiFi";
  133. // break;
  134. // }
  135. //
  136. // NSLog(@"%s (%d) \n\tNetwork Status: %@", name, line, statusString);
  137. //
  138. //} // logNetworkStatus_()
  139. #else
  140. #define logReachabilityFlags(flags)
  141. #define logNetworkStatus(status)
  142. #endif
  143. @interface Reachability (private)
  144. - (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags;
  145. @end
  146. @implementation Reachability
  147. @synthesize key = key_;
  148. // Preclude direct access to ivars.
  149. + (BOOL) accessInstanceVariablesDirectly {
  150. return NO;
  151. } // accessInstanceVariablesDirectly
  152. - (void) dealloc {
  153. [self stopNotifier];
  154. if(reachabilityRef) {
  155. CFRelease(reachabilityRef); reachabilityRef = NULL;
  156. }
  157. self.key = nil;
  158. [super dealloc];
  159. } // dealloc
  160. - (Reachability *) initWithReachabilityRef: (SCNetworkReachabilityRef) ref
  161. {
  162. self = [super init];
  163. if (self != nil)
  164. {
  165. reachabilityRef = ref;
  166. }
  167. return self;
  168. } // initWithReachabilityRef:
  169. #if (defined DEBUG && defined CLASS_DEBUG)
  170. - (NSString *) description {
  171. NSAssert(reachabilityRef, @"-description called with NULL reachabilityRef");
  172. SCNetworkReachabilityFlags flags = 0;
  173. SCNetworkReachabilityGetFlags(reachabilityRef, &flags);
  174. return [NSString stringWithFormat: @"%@\n\t%@", self.key, reachabilityFlags_(flags)];
  175. } // description
  176. #endif
  177. #pragma mark -
  178. #pragma mark Notification Management Methods
  179. //Start listening for reachability notifications on the current run loop
  180. static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info) {
  181. #pragma unused (target, flags)
  182. NSCAssert(info, @"info was NULL in ReachabilityCallback");
  183. NSCAssert([(NSObject*) info isKindOfClass: [Reachability class]], @"info was the wrong class in ReachabilityCallback");
  184. //We're on the main RunLoop, so an NSAutoreleasePool is not necessary, but is added defensively
  185. // in case someone uses the Reachablity object in a different thread.
  186. NSAutoreleasePool* pool = [NSAutoreleasePool new];
  187. // Post a notification to notify the client that the network reachability changed.
  188. [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification
  189. object: (Reachability *) info];
  190. [pool release];
  191. } // ReachabilityCallback()
  192. - (BOOL) startNotifier {
  193. SCNetworkReachabilityContext context = {0, self, NULL, NULL, NULL};
  194. if(SCNetworkReachabilitySetCallback(reachabilityRef, ReachabilityCallback, &context)) {
  195. if(SCNetworkReachabilityScheduleWithRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)) {
  196. return YES;
  197. }
  198. }
  199. return NO;
  200. } // startNotifier
  201. - (void) stopNotifier {
  202. if(reachabilityRef) {
  203. SCNetworkReachabilityUnscheduleFromRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
  204. }
  205. } // stopNotifier
  206. - (BOOL) isEqual: (Reachability *) r {
  207. return [r.key isEqualToString: self.key];
  208. } // isEqual:
  209. #pragma mark -
  210. #pragma mark Reachability Allocation Methods
  211. + (Reachability *) reachabilityWithHostName: (NSString *) hostName {
  212. SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]);
  213. if (ref) {
  214. Reachability *r = [[[self alloc] initWithReachabilityRef: ref] autorelease];
  215. r.key = hostName;
  216. return r;
  217. }
  218. return nil;
  219. } // reachabilityWithHostName
  220. + (NSString *) makeAddressKey: (in_addr_t) addr {
  221. // addr is assumed to be in network byte order.
  222. static const int highShift = 24;
  223. static const int highMidShift = 16;
  224. static const int lowMidShift = 8;
  225. static const in_addr_t mask = 0x000000ff;
  226. addr = ntohl(addr);
  227. return [NSString stringWithFormat: @"%d.%d.%d.%d",
  228. (addr >> highShift) & mask,
  229. (addr >> highMidShift) & mask,
  230. (addr >> lowMidShift) & mask,
  231. addr & mask];
  232. } // makeAddressKey:
  233. + (Reachability *) reachabilityWithAddress: (const struct sockaddr_in *) hostAddress {
  234. SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)hostAddress);
  235. if (ref) {
  236. Reachability *r = [[[self alloc] initWithReachabilityRef: ref] autorelease];
  237. r.key = [self makeAddressKey: hostAddress->sin_addr.s_addr];
  238. return r;
  239. }
  240. return nil;
  241. } // reachabilityWithAddress
  242. + (Reachability *) reachabilityForInternetConnection {
  243. struct sockaddr_in zeroAddress;
  244. bzero(&zeroAddress, sizeof(zeroAddress));
  245. zeroAddress.sin_len = sizeof(zeroAddress);
  246. zeroAddress.sin_family = AF_INET;
  247. Reachability *r = [self reachabilityWithAddress: &zeroAddress];
  248. r.key = kInternetConnection;
  249. return r;
  250. } // reachabilityForInternetConnection
  251. + (Reachability *) reachabilityForLocalWiFi {
  252. struct sockaddr_in localWifiAddress;
  253. bzero(&localWifiAddress, sizeof(localWifiAddress));
  254. localWifiAddress.sin_len = sizeof(localWifiAddress);
  255. localWifiAddress.sin_family = AF_INET;
  256. // IN_LINKLOCALNETNUM is defined in <netinet/in.h> as 169.254.0.0
  257. localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);
  258. Reachability *r = [self reachabilityWithAddress: &localWifiAddress];
  259. r.key = kLocalWiFiConnection;
  260. return r;
  261. } // reachabilityForLocalWiFi
  262. #pragma mark -
  263. #pragma mark Network Flag Handling Methods
  264. #if USE_DDG_EXTENSIONS
  265. //
  266. // iPhone condition codes as reported by a 3GS running iPhone OS v3.0.
  267. // Airplane Mode turned on: Reachability Flag Status: -- -------
  268. // WWAN Active: Reachability Flag Status: WR -t-----
  269. // WWAN Connection required: Reachability Flag Status: WR ct-----
  270. // WiFi turned on: Reachability Flag Status: -R ------- Reachable.
  271. // Local WiFi turned on: Reachability Flag Status: -R xxxxxxd Reachable.
  272. // WiFi turned on: Reachability Flag Status: -R ct----- Connection down. (Non-intuitive, empirically determined answer.)
  273. const SCNetworkReachabilityFlags kConnectionDown = kSCNetworkReachabilityFlagsConnectionRequired |
  274. kSCNetworkReachabilityFlagsTransientConnection;
  275. // WiFi turned on: Reachability Flag Status: -R ct-i--- Reachable but it will require user intervention (e.g. enter a WiFi password).
  276. // WiFi turned on: Reachability Flag Status: -R -t----- Reachable via VPN.
  277. //
  278. // In the below method, an 'x' in the flag status means I don't care about its value.
  279. //
  280. // This method differs from Apple's by testing explicitly for empirically observed values.
  281. // This gives me more confidence in it's correct behavior. Apple's code covers more cases
  282. // than mine. My code covers the cases that occur.
  283. //
  284. - (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags {
  285. if (flags & kSCNetworkReachabilityFlagsReachable) {
  286. // Local WiFi -- Test derived from Apple's code: -localWiFiStatusForFlags:.
  287. if (self.key == kLocalWiFiConnection) {
  288. // Reachability Flag Status: xR xxxxxxd Reachable.
  289. return (flags & kSCNetworkReachabilityFlagsIsDirect) ? kReachableViaWiFi : kNotReachable;
  290. }
  291. // Observed WWAN Values:
  292. // WWAN Active: Reachability Flag Status: WR -t-----
  293. // WWAN Connection required: Reachability Flag Status: WR ct-----
  294. //
  295. // Test Value: Reachability Flag Status: WR xxxxxxx
  296. if (flags & kSCNetworkReachabilityFlagsIsWWAN) { return kReachableViaWWAN; }
  297. // Clear moot bits.
  298. flags &= ~kSCNetworkReachabilityFlagsReachable;
  299. flags &= ~kSCNetworkReachabilityFlagsIsDirect;
  300. flags &= ~kSCNetworkReachabilityFlagsIsLocalAddress; // kInternetConnection is local.
  301. // Reachability Flag Status: -R ct---xx Connection down.
  302. if (flags == kConnectionDown) { return kNotReachable; }
  303. // Reachability Flag Status: -R -t---xx Reachable. WiFi + VPN(is up) (Thank you Ling Wang)
  304. if (flags & kSCNetworkReachabilityFlagsTransientConnection) { return kReachableViaWiFi; }
  305. // Reachability Flag Status: -R -----xx Reachable.
  306. if (flags == 0) { return kReachableViaWiFi; }
  307. // Apple's code tests for dynamic connection types here. I don't.
  308. // If a connection is required, regardless of whether it is on demand or not, it is a WiFi connection.
  309. // If you care whether a connection needs to be brought up, use -isConnectionRequired.
  310. // If you care about whether user intervention is necessary, use -isInterventionRequired.
  311. // If you care about dynamically establishing the connection, use -isConnectionIsOnDemand.
  312. // Reachability Flag Status: -R cxxxxxx Reachable.
  313. if (flags & kSCNetworkReachabilityFlagsConnectionRequired) { return kReachableViaWiFi; }
  314. // Required by the compiler. Should never get here. Default to not connected.
  315. #if (defined DEBUG && defined CLASS_DEBUG)
  316. NSAssert1(NO, @"Uncaught reachability test. Flags: %@", reachabilityFlags_(flags));
  317. #endif
  318. return kNotReachable;
  319. }
  320. // Reachability Flag Status: x- xxxxxxx
  321. return kNotReachable;
  322. } // networkStatusForFlags:
  323. - (NetworkStatus) currentReachabilityStatus {
  324. NSAssert(reachabilityRef, @"currentReachabilityStatus called with NULL reachabilityRef");
  325. SCNetworkReachabilityFlags flags = 0;
  326. NetworkStatus status = kNotReachable;
  327. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  328. // logReachabilityFlags(flags);
  329. status = [self networkStatusForFlags: flags];
  330. return status;
  331. }
  332. return kNotReachable;
  333. } // currentReachabilityStatus
  334. - (BOOL) isReachable {
  335. NSAssert(reachabilityRef, @"isReachable called with NULL reachabilityRef");
  336. SCNetworkReachabilityFlags flags = 0;
  337. NetworkStatus status = kNotReachable;
  338. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  339. // logReachabilityFlags(flags);
  340. status = [self networkStatusForFlags: flags];
  341. // logNetworkStatus(status);
  342. return (kNotReachable != status);
  343. }
  344. return NO;
  345. } // isReachable
  346. - (BOOL) isConnectionRequired {
  347. NSAssert(reachabilityRef, @"isConnectionRequired called with NULL reachabilityRef");
  348. SCNetworkReachabilityFlags flags;
  349. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  350. logReachabilityFlags(flags);
  351. return (flags & kSCNetworkReachabilityFlagsConnectionRequired);
  352. }
  353. return NO;
  354. } // isConnectionRequired
  355. - (BOOL) connectionRequired {
  356. return [self isConnectionRequired];
  357. } // connectionRequired
  358. #endif
  359. #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 30000)
  360. static const SCNetworkReachabilityFlags kOnDemandConnection = kSCNetworkReachabilityFlagsConnectionOnTraffic |
  361. kSCNetworkReachabilityFlagsConnectionOnDemand;
  362. #else
  363. static const SCNetworkReachabilityFlags kOnDemandConnection = kSCNetworkReachabilityFlagsConnectionAutomatic;
  364. #endif
  365. - (BOOL) isConnectionOnDemand {
  366. NSAssert(reachabilityRef, @"isConnectionIsOnDemand called with NULL reachabilityRef");
  367. SCNetworkReachabilityFlags flags;
  368. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  369. logReachabilityFlags(flags);
  370. return ((flags & kSCNetworkReachabilityFlagsConnectionRequired) &&
  371. (flags & kOnDemandConnection));
  372. }
  373. return NO;
  374. } // isConnectionOnDemand
  375. - (BOOL) isInterventionRequired {
  376. NSAssert(reachabilityRef, @"isInterventionRequired called with NULL reachabilityRef");
  377. SCNetworkReachabilityFlags flags;
  378. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  379. logReachabilityFlags(flags);
  380. return ((flags & kSCNetworkReachabilityFlagsConnectionRequired) &&
  381. (flags & kSCNetworkReachabilityFlagsInterventionRequired));
  382. }
  383. return NO;
  384. } // isInterventionRequired
  385. - (BOOL) isReachableViaWWAN {
  386. NSAssert(reachabilityRef, @"isReachableViaWWAN called with NULL reachabilityRef");
  387. SCNetworkReachabilityFlags flags = 0;
  388. NetworkStatus status = kNotReachable;
  389. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  390. logReachabilityFlags(flags);
  391. status = [self networkStatusForFlags: flags];
  392. return (kReachableViaWWAN == status);
  393. }
  394. return NO;
  395. } // isReachableViaWWAN
  396. - (BOOL) isReachableViaWiFi {
  397. NSAssert(reachabilityRef, @"isReachableViaWiFi called with NULL reachabilityRef");
  398. SCNetworkReachabilityFlags flags = 0;
  399. NetworkStatus status = kNotReachable;
  400. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  401. logReachabilityFlags(flags);
  402. status = [self networkStatusForFlags: flags];
  403. return (kReachableViaWiFi == status);
  404. }
  405. return NO;
  406. } // isReachableViaWiFi
  407. - (SCNetworkReachabilityFlags) reachabilityFlags {
  408. NSAssert(reachabilityRef, @"reachabilityFlags called with NULL reachabilityRef");
  409. SCNetworkReachabilityFlags flags = 0;
  410. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  411. logReachabilityFlags(flags);
  412. return flags;
  413. }
  414. return 0;
  415. } // reachabilityFlags
  416. #pragma mark -
  417. #pragma mark Apple's Network Flag Handling Methods
  418. #if !USE_DDG_EXTENSIONS
  419. /*
  420. *
  421. * Apple's Network Status testing code.
  422. * The only changes that have been made are to use the new logReachabilityFlags macro and
  423. * test for local WiFi via the key instead of Apple's boolean. Also, Apple's code was for v3.0 only
  424. * iPhone OS. v2.2.1 and earlier conditional compiling is turned on. Hence, to mirror Apple's behavior,
  425. * set your Base SDK to v3.0 or higher.
  426. *
  427. */
  428. - (NetworkStatus) localWiFiStatusForFlags: (SCNetworkReachabilityFlags) flags
  429. {
  430. logReachabilityFlags(flags);
  431. BOOL retVal = NotReachable;
  432. if((flags & kSCNetworkReachabilityFlagsReachable) && (flags & kSCNetworkReachabilityFlagsIsDirect))
  433. {
  434. retVal = ReachableViaWiFi;
  435. }
  436. return retVal;
  437. }
  438. - (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags
  439. {
  440. logReachabilityFlags(flags);
  441. if (!(flags & kSCNetworkReachabilityFlagsReachable))
  442. {
  443. // if target host is not reachable
  444. return NotReachable;
  445. }
  446. BOOL retVal = NotReachable;
  447. if (!(flags & kSCNetworkReachabilityFlagsConnectionRequired))
  448. {
  449. // if target host is reachable and no connection is required
  450. // then we'll assume (for now) that your on Wi-Fi
  451. retVal = ReachableViaWiFi;
  452. }
  453. #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 30000) // Apple advises you to use the magic number instead of a symbol.
  454. if ((flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ||
  455. (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic))
  456. #else
  457. if (flags & kSCNetworkReachabilityFlagsConnectionAutomatic)
  458. #endif
  459. {
  460. // ... and the connection is on-demand (or on-traffic) if the
  461. // calling application is using the CFSocketStream or higher APIs
  462. if (!(flags & kSCNetworkReachabilityFlagsInterventionRequired))
  463. {
  464. // ... and no [user] intervention is needed
  465. retVal = ReachableViaWiFi;
  466. }
  467. }
  468. if (flags & kSCNetworkReachabilityFlagsIsWWAN)
  469. {
  470. // ... but WWAN connections are OK if the calling application
  471. // is using the CFNetwork (CFSocketStream?) APIs.
  472. retVal = ReachableViaWWAN;
  473. }
  474. return retVal;
  475. }
  476. - (NetworkStatus) currentReachabilityStatus
  477. {
  478. NSAssert(reachabilityRef, @"currentReachabilityStatus called with NULL reachabilityRef");
  479. NetworkStatus retVal = NotReachable;
  480. SCNetworkReachabilityFlags flags;
  481. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags))
  482. {
  483. if(self.key == kLocalWiFiConnection)
  484. {
  485. retVal = [self localWiFiStatusForFlags: flags];
  486. }
  487. else
  488. {
  489. retVal = [self networkStatusForFlags: flags];
  490. }
  491. }
  492. return retVal;
  493. }
  494. - (BOOL) isReachable {
  495. NSAssert(reachabilityRef, @"isReachable called with NULL reachabilityRef");
  496. SCNetworkReachabilityFlags flags = 0;
  497. NetworkStatus status = kNotReachable;
  498. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  499. logReachabilityFlags(flags);
  500. if(self.key == kLocalWiFiConnection) {
  501. status = [self localWiFiStatusForFlags: flags];
  502. } else {
  503. status = [self networkStatusForFlags: flags];
  504. }
  505. return (kNotReachable != status);
  506. }
  507. return NO;
  508. } // isReachable
  509. - (BOOL) isConnectionRequired {
  510. return [self connectionRequired];
  511. } // isConnectionRequired
  512. - (BOOL) connectionRequired {
  513. NSAssert(reachabilityRef, @"connectionRequired called with NULL reachabilityRef");
  514. SCNetworkReachabilityFlags flags;
  515. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  516. logReachabilityFlags(flags);
  517. return (flags & kSCNetworkReachabilityFlagsConnectionRequired);
  518. }
  519. return NO;
  520. } // connectionRequired
  521. #endif
  522. @end