AFNetworkReachabilityManager.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // AFNetworkReachabilityManager.h
  2. // Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. #import <Foundation/Foundation.h>
  22. #if !TARGET_OS_WATCH
  23. #import <SystemConfiguration/SystemConfiguration.h>
  24. typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
  25. AFNetworkReachabilityStatusUnknown = -1,
  26. AFNetworkReachabilityStatusNotReachable = 0,
  27. AFNetworkReachabilityStatusReachableViaWWAN = 1,
  28. AFNetworkReachabilityStatusReachableViaWiFi = 2,
  29. };
  30. NS_ASSUME_NONNULL_BEGIN
  31. /**
  32. `AFNetworkReachabilityManager` monitors the reachability of domains, and addresses for both WWAN and WiFi network interfaces.
  33. Reachability can be used to determine background information about why a network operation failed, or to trigger a network operation retrying when a connection is established. It should not be used to prevent a user from initiating a network request, as it's possible that an initial request may be required to establish reachability.
  34. See Apple's Reachability Sample Code ( https://developer.apple.com/library/ios/samplecode/reachability/ )
  35. @warning Instances of `AFNetworkReachabilityManager` must be started with `-startMonitoring` before reachability status can be determined.
  36. */
  37. @interface AFNetworkReachabilityManager : NSObject
  38. /**
  39. The current network reachability status.
  40. */
  41. @property (readonly, nonatomic, assign) AFNetworkReachabilityStatus networkReachabilityStatus;
  42. /**
  43. Whether or not the network is currently reachable.
  44. */
  45. @property (readonly, nonatomic, assign, getter = isReachable) BOOL reachable;
  46. /**
  47. Whether or not the network is currently reachable via WWAN.
  48. */
  49. @property (readonly, nonatomic, assign, getter = isReachableViaWWAN) BOOL reachableViaWWAN;
  50. /**
  51. Whether or not the network is currently reachable via WiFi.
  52. */
  53. @property (readonly, nonatomic, assign, getter = isReachableViaWiFi) BOOL reachableViaWiFi;
  54. ///---------------------
  55. /// @name Initialization
  56. ///---------------------
  57. /**
  58. Returns the shared network reachability manager.
  59. */
  60. + (instancetype)sharedManager;
  61. /**
  62. Creates and returns a network reachability manager with the default socket address.
  63. @return An initialized network reachability manager, actively monitoring the default socket address.
  64. */
  65. + (instancetype)manager;
  66. /**
  67. Creates and returns a network reachability manager for the specified domain.
  68. @param domain The domain used to evaluate network reachability.
  69. @return An initialized network reachability manager, actively monitoring the specified domain.
  70. */
  71. + (instancetype)managerForDomain:(NSString *)domain;
  72. /**
  73. Creates and returns a network reachability manager for the socket address.
  74. @param address The socket address (`sockaddr_in6`) used to evaluate network reachability.
  75. @return An initialized network reachability manager, actively monitoring the specified socket address.
  76. */
  77. + (instancetype)managerForAddress:(const void *)address;
  78. /**
  79. Initializes an instance of a network reachability manager from the specified reachability object.
  80. @param reachability The reachability object to monitor.
  81. @return An initialized network reachability manager, actively monitoring the specified reachability.
  82. */
  83. - (instancetype)initWithReachability:(SCNetworkReachabilityRef)reachability NS_DESIGNATED_INITIALIZER;
  84. /**
  85. * Unavailable initializer
  86. */
  87. + (instancetype)new NS_UNAVAILABLE;
  88. /**
  89. * Unavailable initializer
  90. */
  91. - (instancetype)init NS_UNAVAILABLE;
  92. ///--------------------------------------------------
  93. /// @name Starting & Stopping Reachability Monitoring
  94. ///--------------------------------------------------
  95. /**
  96. Starts monitoring for changes in network reachability status.
  97. */
  98. - (void)startMonitoring;
  99. /**
  100. Stops monitoring for changes in network reachability status.
  101. */
  102. - (void)stopMonitoring;
  103. ///-------------------------------------------------
  104. /// @name Getting Localized Reachability Description
  105. ///-------------------------------------------------
  106. /**
  107. Returns a localized string representation of the current network reachability status.
  108. */
  109. - (NSString *)localizedNetworkReachabilityStatusString;
  110. ///---------------------------------------------------
  111. /// @name Setting Network Reachability Change Callback
  112. ///---------------------------------------------------
  113. /**
  114. Sets a callback to be executed when the network availability of the `baseURL` host changes.
  115. @param block A block object to be executed when the network availability of the `baseURL` host changes.. This block has no return value and takes a single argument which represents the various reachability states from the device to the `baseURL`.
  116. */
  117. - (void)setReachabilityStatusChangeBlock:(nullable void (^)(AFNetworkReachabilityStatus status))block;
  118. @end
  119. ///----------------
  120. /// @name Constants
  121. ///----------------
  122. /**
  123. ## Network Reachability
  124. The following constants are provided by `AFNetworkReachabilityManager` as possible network reachability statuses.
  125. enum {
  126. AFNetworkReachabilityStatusUnknown,
  127. AFNetworkReachabilityStatusNotReachable,
  128. AFNetworkReachabilityStatusReachableViaWWAN,
  129. AFNetworkReachabilityStatusReachableViaWiFi,
  130. }
  131. `AFNetworkReachabilityStatusUnknown`
  132. The `baseURL` host reachability is not known.
  133. `AFNetworkReachabilityStatusNotReachable`
  134. The `baseURL` host cannot be reached.
  135. `AFNetworkReachabilityStatusReachableViaWWAN`
  136. The `baseURL` host can be reached via a cellular connection, such as EDGE or GPRS.
  137. `AFNetworkReachabilityStatusReachableViaWiFi`
  138. The `baseURL` host can be reached via a Wi-Fi connection.
  139. ### Keys for Notification UserInfo Dictionary
  140. Strings that are used as keys in a `userInfo` dictionary in a network reachability status change notification.
  141. `AFNetworkingReachabilityNotificationStatusItem`
  142. A key in the userInfo dictionary in a `AFNetworkingReachabilityDidChangeNotification` notification.
  143. The corresponding value is an `NSNumber` object representing the `AFNetworkReachabilityStatus` value for the current reachability status.
  144. */
  145. ///--------------------
  146. /// @name Notifications
  147. ///--------------------
  148. /**
  149. Posted when network reachability changes.
  150. This notification assigns no notification object. The `userInfo` dictionary contains an `NSNumber` object under the `AFNetworkingReachabilityNotificationStatusItem` key, representing the `AFNetworkReachabilityStatus` value for the current network reachability.
  151. @warning In order for network reachability to be monitored, include the `SystemConfiguration` framework in the active target's "Link Binary With Library" build phase, and add `#import <SystemConfiguration/SystemConfiguration.h>` to the header prefix of the project (`Prefix.pch`).
  152. */
  153. FOUNDATION_EXPORT NSString * const AFNetworkingReachabilityDidChangeNotification;
  154. FOUNDATION_EXPORT NSString * const AFNetworkingReachabilityNotificationStatusItem;
  155. ///--------------------
  156. /// @name Functions
  157. ///--------------------
  158. /**
  159. Returns a localized string representation of an `AFNetworkReachabilityStatus` value.
  160. */
  161. FOUNDATION_EXPORT NSString * AFStringFromNetworkReachabilityStatus(AFNetworkReachabilityStatus status);
  162. NS_ASSUME_NONNULL_END
  163. #endif