天天省钱快报

Reachability.m 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. Copyright (C) 2016 Apple Inc. All Rights Reserved.
  3. See LICENSE.txt for this sample’s licensing information
  4. Abstract:
  5. Basic demonstration of how to use the SystemConfiguration Reachablity APIs.
  6. */
  7. #import <arpa/inet.h>
  8. #import <ifaddrs.h>
  9. #import <netdb.h>
  10. #import <sys/socket.h>
  11. #import <netinet/in.h>
  12. #import <CoreFoundation/CoreFoundation.h>
  13. #import "Reachability.h"
  14. #import <CoreTelephony/CTTelephonyNetworkInfo.h>
  15. #pragma mark IPv6 Support
  16. //Reachability fully support IPv6. For full details, see ReadMe.md.
  17. NSString *kReachabilityChangedNotification = @"kNetworkReachabilityChangedNotification";
  18. #pragma mark - Supporting functions
  19. #define kShouldPrintReachabilityFlags 1
  20. static void PrintReachabilityFlags(SCNetworkReachabilityFlags flags, const char* comment)
  21. {
  22. #if kShouldPrintReachabilityFlags
  23. // NSLog(@"Reachability Flag Status: %c%c %c%c%c%c%c%c%c %s\n",
  24. // (flags & kSCNetworkReachabilityFlagsIsWWAN) ? 'W' : '-',
  25. // (flags & kSCNetworkReachabilityFlagsReachable) ? 'R' : '-',
  26. //
  27. // (flags & kSCNetworkReachabilityFlagsTransientConnection) ? 't' : '-',
  28. // (flags & kSCNetworkReachabilityFlagsConnectionRequired) ? 'c' : '-',
  29. // (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) ? 'C' : '-',
  30. // (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-',
  31. // (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ? 'D' : '-',
  32. // (flags & kSCNetworkReachabilityFlagsIsLocalAddress) ? 'l' : '-',
  33. // (flags & kSCNetworkReachabilityFlagsIsDirect) ? 'd' : '-',
  34. // comment
  35. // );
  36. #endif
  37. }
  38. static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info)
  39. {
  40. #pragma unused (target, flags)
  41. NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback");
  42. NSCAssert([(__bridge NSObject*) info isKindOfClass: [Reachability class]], @"info was wrong class in ReachabilityCallback");
  43. Reachability* noteObject = (__bridge Reachability *)info;
  44. // Post a notification to notify the client that the network reachability changed.
  45. [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification object: noteObject];
  46. }
  47. #pragma mark - Reachability implementation
  48. @implementation Reachability
  49. {
  50. SCNetworkReachabilityRef _reachabilityRef;
  51. }
  52. + (instancetype)reachabilityWithHostName:(NSString *)hostName
  53. {
  54. Reachability* returnValue = NULL;
  55. SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]);
  56. if (reachability != NULL)
  57. {
  58. returnValue= [[self alloc] init];
  59. if (returnValue != NULL)
  60. {
  61. returnValue->_reachabilityRef = reachability;
  62. }
  63. else {
  64. CFRelease(reachability);
  65. }
  66. }
  67. return returnValue;
  68. }
  69. + (instancetype)reachabilityWithAddress:(const struct sockaddr *)hostAddress
  70. {
  71. SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, hostAddress);
  72. Reachability* returnValue = NULL;
  73. if (reachability != NULL)
  74. {
  75. returnValue = [[self alloc] init];
  76. if (returnValue != NULL)
  77. {
  78. returnValue->_reachabilityRef = reachability;
  79. }
  80. else {
  81. CFRelease(reachability);
  82. }
  83. }
  84. return returnValue;
  85. }
  86. + (instancetype)reachabilityForInternetConnection
  87. {
  88. struct sockaddr_in zeroAddress;
  89. bzero(&zeroAddress, sizeof(zeroAddress));
  90. zeroAddress.sin_len = sizeof(zeroAddress);
  91. zeroAddress.sin_family = AF_INET;
  92. return [self reachabilityWithAddress: (const struct sockaddr *) &zeroAddress];
  93. }
  94. #pragma mark reachabilityForLocalWiFi
  95. //reachabilityForLocalWiFi has been removed from the sample. See ReadMe.md for more information.
  96. //+ (instancetype)reachabilityForLocalWiFi
  97. #pragma mark - Start and stop notifier
  98. - (BOOL)startNotifier
  99. {
  100. BOOL returnValue = NO;
  101. SCNetworkReachabilityContext context = {0, (__bridge void *)(self), NULL, NULL, NULL};
  102. if (SCNetworkReachabilitySetCallback(_reachabilityRef, ReachabilityCallback, &context))
  103. {
  104. if (SCNetworkReachabilityScheduleWithRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode))
  105. {
  106. returnValue = YES;
  107. }
  108. }
  109. return returnValue;
  110. }
  111. - (void)stopNotifier
  112. {
  113. if (_reachabilityRef != NULL)
  114. {
  115. SCNetworkReachabilityUnscheduleFromRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
  116. }
  117. }
  118. - (void)dealloc
  119. {
  120. [self stopNotifier];
  121. if (_reachabilityRef != NULL)
  122. {
  123. CFRelease(_reachabilityRef);
  124. }
  125. }
  126. #pragma mark - Network Flag Handling
  127. - (NetworkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags
  128. {
  129. PrintReachabilityFlags(flags, "networkStatusForFlags");
  130. if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
  131. {
  132. // The target host is not reachable.
  133. return NotReachable;
  134. }
  135. NetworkStatus returnValue = NotReachable;
  136. if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
  137. {
  138. /*
  139. If the target host is reachable and no connection is required then we'll assume (for now) that you're on Wi-Fi...
  140. */
  141. returnValue = ReachableViaWiFi;
  142. }
  143. if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
  144. (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
  145. {
  146. /*
  147. ... and the connection is on-demand (or on-traffic) if the calling application is using the CFSocketStream or higher APIs...
  148. */
  149. if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
  150. {
  151. /*
  152. ... and no [user] intervention is needed...
  153. */
  154. returnValue = ReachableViaWiFi;
  155. }
  156. }
  157. if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
  158. {
  159. /*
  160. ... but WWAN connections are OK if the calling application is using the CFNetwork APIs.
  161. */
  162. returnValue = ReachableViaWWAN;
  163. float IOS_VERSION=[[[UIDevice currentDevice] systemVersion] floatValue];
  164. NSLog(@"版本号:%lf",IOS_VERSION);
  165. if (IOS_VERSION >= 7.0) {
  166. CTTelephonyNetworkInfo *phonyNetwork = [[CTTelephonyNetworkInfo alloc] init];
  167. NSString *currentStr = phonyNetwork.currentRadioAccessTechnology;
  168. if (currentStr) {
  169. if ([currentStr isEqualToString:CTRadioAccessTechnologyLTE]) {
  170. return kRaeachableVia4G;
  171. }else if ([currentStr isEqualToString:CTRadioAccessTechnologyGPRS]|| [currentStr isEqualToString:CTRadioAccessTechnologyEdge]){
  172. return kReachableVia2G;
  173. }else{
  174. return kReachableVia3G;
  175. }
  176. }
  177. }
  178. if ((flags & kSCNetworkReachabilityFlagsTransientConnection) == kSCNetworkReachabilityFlagsTransientConnection) {
  179. if((flags & kSCNetworkReachabilityFlagsConnectionRequired) == kSCNetworkReachabilityFlagsConnectionRequired) {
  180. return kReachableVia2G;
  181. }
  182. return kReachableVia3G;
  183. }
  184. return ReachableViaWWAN;
  185. }
  186. return returnValue;
  187. }
  188. - (BOOL)connectionRequired
  189. {
  190. NSAssert(_reachabilityRef != NULL, @"connectionRequired called with NULL reachabilityRef");
  191. SCNetworkReachabilityFlags flags;
  192. if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags))
  193. {
  194. return (flags & kSCNetworkReachabilityFlagsConnectionRequired);
  195. }
  196. return NO;
  197. }
  198. - (NetworkStatus)currentReachabilityStatus
  199. {
  200. NSAssert(_reachabilityRef != NULL, @"currentNetworkStatus called with NULL SCNetworkReachabilityRef");
  201. NetworkStatus returnValue = NotReachable;
  202. SCNetworkReachabilityFlags flags;
  203. if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags))
  204. {
  205. returnValue = [self networkStatusForFlags:flags];
  206. }
  207. return returnValue;
  208. }
  209. @end