PageRenderTime 90ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/IOSBoilerplate/Reachability/Reachability.m

https://github.com/HueMachine/iOS-boilerplate
Objective C | 814 lines | 378 code | 278 blank | 158 comment | 50 complexity | ee39cbaee5f108bf1aee3c76984102f0 MD5 | raw file
  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. NSString *statusString = nil;
  122. switch (status) {
  123. case kNotReachable:
  124. statusString = [NSString stringWithString: @"Not Reachable"];
  125. break;
  126. case kReachableViaWWAN:
  127. statusString = [NSString stringWithString: @"Reachable via WWAN"];
  128. break;
  129. case kReachableViaWiFi:
  130. statusString = [NSString stringWithString: @"Reachable via WiFi"];
  131. break;
  132. }
  133. NSLog(@"%s (%d) \n\tNetwork Status: %@", name, line, statusString);
  134. } // logNetworkStatus_()
  135. #else
  136. #define logReachabilityFlags(flags)
  137. #define logNetworkStatus(status)
  138. #endif
  139. @interface Reachability (private)
  140. - (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags;
  141. @end
  142. @implementation Reachability
  143. @synthesize key = key_;
  144. // Preclude direct access to ivars.
  145. + (BOOL) accessInstanceVariablesDirectly {
  146. return NO;
  147. } // accessInstanceVariablesDirectly
  148. - (void) dealloc {
  149. [self stopNotifier];
  150. if(reachabilityRef) {
  151. CFRelease(reachabilityRef); reachabilityRef = NULL;
  152. }
  153. self.key = nil;
  154. [super dealloc];
  155. } // dealloc
  156. - (Reachability *) initWithReachabilityRef: (SCNetworkReachabilityRef) ref
  157. {
  158. self = [super init];
  159. if (self != nil)
  160. {
  161. reachabilityRef = ref;
  162. }
  163. return self;
  164. } // initWithReachabilityRef:
  165. #if (defined DEBUG && defined CLASS_DEBUG)
  166. - (NSString *) description {
  167. NSAssert(reachabilityRef, @"-description called with NULL reachabilityRef");
  168. SCNetworkReachabilityFlags flags = 0;
  169. SCNetworkReachabilityGetFlags(reachabilityRef, &flags);
  170. return [NSString stringWithFormat: @"%@\n\t%@", self.key, reachabilityFlags_(flags)];
  171. } // description
  172. #endif
  173. #pragma mark -
  174. #pragma mark Notification Management Methods
  175. //Start listening for reachability notifications on the current run loop
  176. static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info) {
  177. #pragma unused (target, flags)
  178. NSCAssert(info, @"info was NULL in ReachabilityCallback");
  179. NSCAssert([(NSObject*) info isKindOfClass: [Reachability class]], @"info was the wrong class in ReachabilityCallback");
  180. //We're on the main RunLoop, so an NSAutoreleasePool is not necessary, but is added defensively
  181. // in case someone uses the Reachablity object in a different thread.
  182. NSAutoreleasePool* pool = [NSAutoreleasePool new];
  183. // Post a notification to notify the client that the network reachability changed.
  184. [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification
  185. object: (Reachability *) info];
  186. [pool release];
  187. } // ReachabilityCallback()
  188. - (BOOL) startNotifier {
  189. SCNetworkReachabilityContext context = {0, self, NULL, NULL, NULL};
  190. if(SCNetworkReachabilitySetCallback(reachabilityRef, ReachabilityCallback, &context)) {
  191. if(SCNetworkReachabilityScheduleWithRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)) {
  192. return YES;
  193. }
  194. }
  195. return NO;
  196. } // startNotifier
  197. - (void) stopNotifier {
  198. if(reachabilityRef) {
  199. SCNetworkReachabilityUnscheduleFromRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
  200. }
  201. } // stopNotifier
  202. - (BOOL) isEqual: (Reachability *) r {
  203. return [r.key isEqualToString: self.key];
  204. } // isEqual:
  205. #pragma mark -
  206. #pragma mark Reachability Allocation Methods
  207. + (Reachability *) reachabilityWithHostName: (NSString *) hostName {
  208. SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]);
  209. if (ref) {
  210. Reachability *r = [[[self alloc] initWithReachabilityRef: ref] autorelease];
  211. r.key = hostName;
  212. return r;
  213. }
  214. return nil;
  215. } // reachabilityWithHostName
  216. + (NSString *) makeAddressKey: (in_addr_t) addr {
  217. // addr is assumed to be in network byte order.
  218. static const int highShift = 24;
  219. static const int highMidShift = 16;
  220. static const int lowMidShift = 8;
  221. static const in_addr_t mask = 0x000000ff;
  222. addr = ntohl(addr);
  223. return [NSString stringWithFormat: @"%d.%d.%d.%d",
  224. (addr >> highShift) & mask,
  225. (addr >> highMidShift) & mask,
  226. (addr >> lowMidShift) & mask,
  227. addr & mask];
  228. } // makeAddressKey:
  229. + (Reachability *) reachabilityWithAddress: (const struct sockaddr_in *) hostAddress {
  230. SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)hostAddress);
  231. if (ref) {
  232. Reachability *r = [[[self alloc] initWithReachabilityRef: ref] autorelease];
  233. r.key = [self makeAddressKey: hostAddress->sin_addr.s_addr];
  234. return r;
  235. }
  236. return nil;
  237. } // reachabilityWithAddress
  238. + (Reachability *) reachabilityForInternetConnection {
  239. struct sockaddr_in zeroAddress;
  240. bzero(&zeroAddress, sizeof(zeroAddress));
  241. zeroAddress.sin_len = sizeof(zeroAddress);
  242. zeroAddress.sin_family = AF_INET;
  243. Reachability *r = [self reachabilityWithAddress: &zeroAddress];
  244. r.key = kInternetConnection;
  245. return r;
  246. } // reachabilityForInternetConnection
  247. + (Reachability *) reachabilityForLocalWiFi {
  248. struct sockaddr_in localWifiAddress;
  249. bzero(&localWifiAddress, sizeof(localWifiAddress));
  250. localWifiAddress.sin_len = sizeof(localWifiAddress);
  251. localWifiAddress.sin_family = AF_INET;
  252. // IN_LINKLOCALNETNUM is defined in <netinet/in.h> as 169.254.0.0
  253. localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);
  254. Reachability *r = [self reachabilityWithAddress: &localWifiAddress];
  255. r.key = kLocalWiFiConnection;
  256. return r;
  257. } // reachabilityForLocalWiFi
  258. #pragma mark -
  259. #pragma mark Network Flag Handling Methods
  260. #if USE_DDG_EXTENSIONS
  261. //
  262. // iPhone condition codes as reported by a 3GS running iPhone OS v3.0.
  263. // Airplane Mode turned on: Reachability Flag Status: -- -------
  264. // WWAN Active: Reachability Flag Status: WR -t-----
  265. // WWAN Connection required: Reachability Flag Status: WR ct-----
  266. // WiFi turned on: Reachability Flag Status: -R ------- Reachable.
  267. // Local WiFi turned on: Reachability Flag Status: -R xxxxxxd Reachable.
  268. // WiFi turned on: Reachability Flag Status: -R ct----- Connection down. (Non-intuitive, empirically determined answer.)
  269. const SCNetworkReachabilityFlags kConnectionDown = kSCNetworkReachabilityFlagsConnectionRequired |
  270. kSCNetworkReachabilityFlagsTransientConnection;
  271. // WiFi turned on: Reachability Flag Status: -R ct-i--- Reachable but it will require user intervention (e.g. enter a WiFi password).
  272. // WiFi turned on: Reachability Flag Status: -R -t----- Reachable via VPN.
  273. //
  274. // In the below method, an 'x' in the flag status means I don't care about its value.
  275. //
  276. // This method differs from Apple's by testing explicitly for empirically observed values.
  277. // This gives me more confidence in it's correct behavior. Apple's code covers more cases
  278. // than mine. My code covers the cases that occur.
  279. //
  280. - (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags {
  281. if (flags & kSCNetworkReachabilityFlagsReachable) {
  282. // Local WiFi -- Test derived from Apple's code: -localWiFiStatusForFlags:.
  283. if (self.key == kLocalWiFiConnection) {
  284. // Reachability Flag Status: xR xxxxxxd Reachable.
  285. return (flags & kSCNetworkReachabilityFlagsIsDirect) ? kReachableViaWiFi : kNotReachable;
  286. }
  287. // Observed WWAN Values:
  288. // WWAN Active: Reachability Flag Status: WR -t-----
  289. // WWAN Connection required: Reachability Flag Status: WR ct-----
  290. //
  291. // Test Value: Reachability Flag Status: WR xxxxxxx
  292. if (flags & kSCNetworkReachabilityFlagsIsWWAN) { return kReachableViaWWAN; }
  293. // Clear moot bits.
  294. flags &= ~kSCNetworkReachabilityFlagsReachable;
  295. flags &= ~kSCNetworkReachabilityFlagsIsDirect;
  296. flags &= ~kSCNetworkReachabilityFlagsIsLocalAddress; // kInternetConnection is local.
  297. // Reachability Flag Status: -R ct---xx Connection down.
  298. if (flags == kConnectionDown) { return kNotReachable; }
  299. // Reachability Flag Status: -R -t---xx Reachable. WiFi + VPN(is up) (Thank you Ling Wang)
  300. if (flags & kSCNetworkReachabilityFlagsTransientConnection) { return kReachableViaWiFi; }
  301. // Reachability Flag Status: -R -----xx Reachable.
  302. if (flags == 0) { return kReachableViaWiFi; }
  303. // Apple's code tests for dynamic connection types here. I don't.
  304. // If a connection is required, regardless of whether it is on demand or not, it is a WiFi connection.
  305. // If you care whether a connection needs to be brought up, use -isConnectionRequired.
  306. // If you care about whether user intervention is necessary, use -isInterventionRequired.
  307. // If you care about dynamically establishing the connection, use -isConnectionIsOnDemand.
  308. // Reachability Flag Status: -R cxxxxxx Reachable.
  309. if (flags & kSCNetworkReachabilityFlagsConnectionRequired) { return kReachableViaWiFi; }
  310. // Required by the compiler. Should never get here. Default to not connected.
  311. #if (defined DEBUG && defined CLASS_DEBUG)
  312. NSAssert1(NO, @"Uncaught reachability test. Flags: %@", reachabilityFlags_(flags));
  313. #endif
  314. return kNotReachable;
  315. }
  316. // Reachability Flag Status: x- xxxxxxx
  317. return kNotReachable;
  318. } // networkStatusForFlags:
  319. - (NetworkStatus) currentReachabilityStatus {
  320. NSAssert(reachabilityRef, @"currentReachabilityStatus called with NULL reachabilityRef");
  321. SCNetworkReachabilityFlags flags = 0;
  322. NetworkStatus status = kNotReachable;
  323. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  324. // logReachabilityFlags(flags);
  325. status = [self networkStatusForFlags: flags];
  326. return status;
  327. }
  328. return kNotReachable;
  329. } // currentReachabilityStatus
  330. - (BOOL) isReachable {
  331. NSAssert(reachabilityRef, @"isReachable called with NULL reachabilityRef");
  332. SCNetworkReachabilityFlags flags = 0;
  333. NetworkStatus status = kNotReachable;
  334. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  335. // logReachabilityFlags(flags);
  336. status = [self networkStatusForFlags: flags];
  337. // logNetworkStatus(status);
  338. return (kNotReachable != status);
  339. }
  340. return NO;
  341. } // isReachable
  342. - (BOOL) isConnectionRequired {
  343. NSAssert(reachabilityRef, @"isConnectionRequired called with NULL reachabilityRef");
  344. SCNetworkReachabilityFlags flags;
  345. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  346. logReachabilityFlags(flags);
  347. return (flags & kSCNetworkReachabilityFlagsConnectionRequired);
  348. }
  349. return NO;
  350. } // isConnectionRequired
  351. - (BOOL) connectionRequired {
  352. return [self isConnectionRequired];
  353. } // connectionRequired
  354. #endif
  355. #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 30000)
  356. static const SCNetworkReachabilityFlags kOnDemandConnection = kSCNetworkReachabilityFlagsConnectionOnTraffic |
  357. kSCNetworkReachabilityFlagsConnectionOnDemand;
  358. #else
  359. static const SCNetworkReachabilityFlags kOnDemandConnection = kSCNetworkReachabilityFlagsConnectionAutomatic;
  360. #endif
  361. - (BOOL) isConnectionOnDemand {
  362. NSAssert(reachabilityRef, @"isConnectionIsOnDemand called with NULL reachabilityRef");
  363. SCNetworkReachabilityFlags flags;
  364. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  365. logReachabilityFlags(flags);
  366. return ((flags & kSCNetworkReachabilityFlagsConnectionRequired) &&
  367. (flags & kOnDemandConnection));
  368. }
  369. return NO;
  370. } // isConnectionOnDemand
  371. - (BOOL) isInterventionRequired {
  372. NSAssert(reachabilityRef, @"isInterventionRequired called with NULL reachabilityRef");
  373. SCNetworkReachabilityFlags flags;
  374. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  375. logReachabilityFlags(flags);
  376. return ((flags & kSCNetworkReachabilityFlagsConnectionRequired) &&
  377. (flags & kSCNetworkReachabilityFlagsInterventionRequired));
  378. }
  379. return NO;
  380. } // isInterventionRequired
  381. - (BOOL) isReachableViaWWAN {
  382. NSAssert(reachabilityRef, @"isReachableViaWWAN called with NULL reachabilityRef");
  383. SCNetworkReachabilityFlags flags = 0;
  384. NetworkStatus status = kNotReachable;
  385. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  386. logReachabilityFlags(flags);
  387. status = [self networkStatusForFlags: flags];
  388. return (kReachableViaWWAN == status);
  389. }
  390. return NO;
  391. } // isReachableViaWWAN
  392. - (BOOL) isReachableViaWiFi {
  393. NSAssert(reachabilityRef, @"isReachableViaWiFi called with NULL reachabilityRef");
  394. SCNetworkReachabilityFlags flags = 0;
  395. NetworkStatus status = kNotReachable;
  396. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  397. logReachabilityFlags(flags);
  398. status = [self networkStatusForFlags: flags];
  399. return (kReachableViaWiFi == status);
  400. }
  401. return NO;
  402. } // isReachableViaWiFi
  403. - (SCNetworkReachabilityFlags) reachabilityFlags {
  404. NSAssert(reachabilityRef, @"reachabilityFlags called with NULL reachabilityRef");
  405. SCNetworkReachabilityFlags flags = 0;
  406. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  407. logReachabilityFlags(flags);
  408. return flags;
  409. }
  410. return 0;
  411. } // reachabilityFlags
  412. #pragma mark -
  413. #pragma mark Apple's Network Flag Handling Methods
  414. #if !USE_DDG_EXTENSIONS
  415. /*
  416. *
  417. * Apple's Network Status testing code.
  418. * The only changes that have been made are to use the new logReachabilityFlags macro and
  419. * test for local WiFi via the key instead of Apple's boolean. Also, Apple's code was for v3.0 only
  420. * iPhone OS. v2.2.1 and earlier conditional compiling is turned on. Hence, to mirror Apple's behavior,
  421. * set your Base SDK to v3.0 or higher.
  422. *
  423. */
  424. - (NetworkStatus) localWiFiStatusForFlags: (SCNetworkReachabilityFlags) flags
  425. {
  426. logReachabilityFlags(flags);
  427. BOOL retVal = NotReachable;
  428. if((flags & kSCNetworkReachabilityFlagsReachable) && (flags & kSCNetworkReachabilityFlagsIsDirect))
  429. {
  430. retVal = ReachableViaWiFi;
  431. }
  432. return retVal;
  433. }
  434. - (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags
  435. {
  436. logReachabilityFlags(flags);
  437. if (!(flags & kSCNetworkReachabilityFlagsReachable))
  438. {
  439. // if target host is not reachable
  440. return NotReachable;
  441. }
  442. BOOL retVal = NotReachable;
  443. if (!(flags & kSCNetworkReachabilityFlagsConnectionRequired))
  444. {
  445. // if target host is reachable and no connection is required
  446. // then we'll assume (for now) that your on Wi-Fi
  447. retVal = ReachableViaWiFi;
  448. }
  449. #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 30000) // Apple advises you to use the magic number instead of a symbol.
  450. if ((flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ||
  451. (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic))
  452. #else
  453. if (flags & kSCNetworkReachabilityFlagsConnectionAutomatic)
  454. #endif
  455. {
  456. // ... and the connection is on-demand (or on-traffic) if the
  457. // calling application is using the CFSocketStream or higher APIs
  458. if (!(flags & kSCNetworkReachabilityFlagsInterventionRequired))
  459. {
  460. // ... and no [user] intervention is needed
  461. retVal = ReachableViaWiFi;
  462. }
  463. }
  464. if (flags & kSCNetworkReachabilityFlagsIsWWAN)
  465. {
  466. // ... but WWAN connections are OK if the calling application
  467. // is using the CFNetwork (CFSocketStream?) APIs.
  468. retVal = ReachableViaWWAN;
  469. }
  470. return retVal;
  471. }
  472. - (NetworkStatus) currentReachabilityStatus
  473. {
  474. NSAssert(reachabilityRef, @"currentReachabilityStatus called with NULL reachabilityRef");
  475. NetworkStatus retVal = NotReachable;
  476. SCNetworkReachabilityFlags flags;
  477. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags))
  478. {
  479. if(self.key == kLocalWiFiConnection)
  480. {
  481. retVal = [self localWiFiStatusForFlags: flags];
  482. }
  483. else
  484. {
  485. retVal = [self networkStatusForFlags: flags];
  486. }
  487. }
  488. return retVal;
  489. }
  490. - (BOOL) isReachable {
  491. NSAssert(reachabilityRef, @"isReachable called with NULL reachabilityRef");
  492. SCNetworkReachabilityFlags flags = 0;
  493. NetworkStatus status = kNotReachable;
  494. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  495. logReachabilityFlags(flags);
  496. if(self.key == kLocalWiFiConnection) {
  497. status = [self localWiFiStatusForFlags: flags];
  498. } else {
  499. status = [self networkStatusForFlags: flags];
  500. }
  501. return (kNotReachable != status);
  502. }
  503. return NO;
  504. } // isReachable
  505. - (BOOL) isConnectionRequired {
  506. return [self connectionRequired];
  507. } // isConnectionRequired
  508. - (BOOL) connectionRequired {
  509. NSAssert(reachabilityRef, @"connectionRequired called with NULL reachabilityRef");
  510. SCNetworkReachabilityFlags flags;
  511. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
  512. logReachabilityFlags(flags);
  513. return (flags & kSCNetworkReachabilityFlagsConnectionRequired);
  514. }
  515. return NO;
  516. } // connectionRequired
  517. #endif
  518. @end