AFNetworkReachabilityManager.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // AFNetworkReachabilityManager.h
  2. // Copyright (c) 2011–2015 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. #ifndef NS_DESIGNATED_INITIALIZER
  25. #if __has_attribute(objc_designated_initializer)
  26. #define NS_DESIGNATED_INITIALIZER __attribute__((objc_designated_initializer))
  27. #else
  28. #define NS_DESIGNATED_INITIALIZER
  29. #endif
  30. #endif
  31. typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
  32. AFNetworkReachabilityStatusUnknown = -1,
  33. AFNetworkReachabilityStatusNotReachable = 0,
  34. AFNetworkReachabilityStatusReachableViaWWAN = 1,
  35. AFNetworkReachabilityStatusReachableViaWiFi = 2,
  36. };
  37. NS_ASSUME_NONNULL_BEGIN
  38. /**
  39. `AFNetworkReachabilityManager` monitors the reachability of domains, and addresses for both WWAN and WiFi network interfaces.
  40. 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.
  41. See Apple's Reachability Sample Code (https://developer.apple.com/library/ios/samplecode/reachability/)
  42. @warning Instances of `AFNetworkReachabilityManager` must be started with `-startMonitoring` before reachability status can be determined.
  43. */
  44. @interface AFNetworkReachabilityManager : NSObject
  45. /**
  46. The current network reachability status.
  47. */
  48. @property (readonly, nonatomic, assign) AFNetworkReachabilityStatus networkReachabilityStatus;
  49. /**
  50. Whether or not the network is currently reachable.
  51. */
  52. @property (readonly, nonatomic, assign, getter = isReachable) BOOL reachable;
  53. /**
  54. Whether or not the network is currently reachable via WWAN.
  55. */
  56. @property (readonly, nonatomic, assign, getter = isReachableViaWWAN) BOOL reachableViaWWAN;
  57. /**
  58. Whether or not the network is currently reachable via WiFi.
  59. */
  60. @property (readonly, nonatomic, assign, getter = isReachableViaWiFi) BOOL reachableViaWiFi;
  61. ///---------------------
  62. /// @name Initialization
  63. ///---------------------
  64. /**
  65. Returns the shared network reachability manager.
  66. */
  67. + (instancetype)sharedManager;
  68. /**
  69. Creates and returns a network reachability manager for the specified domain.
  70. @param domain The domain used to evaluate network reachability.
  71. @return An initialized network reachability manager, actively monitoring the specified domain.
  72. */
  73. + (instancetype)managerForDomain:(NSString *)domain;
  74. /**
  75. Creates and returns a network reachability manager for the socket address.
  76. @param address The socket address (`sockaddr_in`) used to evaluate network reachability.
  77. @return An initialized network reachability manager, actively monitoring the specified socket address.
  78. */
  79. + (instancetype)managerForAddress:(const void *)address;
  80. /**
  81. Initializes an instance of a network reachability manager from the specified reachability object.
  82. @param reachability The reachability object to monitor.
  83. @return An initialized network reachability manager, actively monitoring the specified reachability.
  84. */
  85. - (instancetype)initWithReachability:(SCNetworkReachabilityRef)reachability NS_DESIGNATED_INITIALIZER;
  86. ///--------------------------------------------------
  87. /// @name Starting & Stopping Reachability Monitoring
  88. ///--------------------------------------------------
  89. /**
  90. Starts monitoring for changes in network reachability status.
  91. */
  92. - (void)startMonitoring;
  93. /**
  94. Stops monitoring for changes in network reachability status.
  95. */
  96. - (void)stopMonitoring;
  97. ///-------------------------------------------------
  98. /// @name Getting Localized Reachability Description
  99. ///-------------------------------------------------
  100. /**
  101. Returns a localized string representation of the current network reachability status.
  102. */
  103. - (NSString *)localizedNetworkReachabilityStatusString;
  104. ///---------------------------------------------------
  105. /// @name Setting Network Reachability Change Callback
  106. ///---------------------------------------------------
  107. /**
  108. Sets a callback to be executed when the network availability of the `baseURL` host changes.
  109. @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`.
  110. */
  111. - (void)setReachabilityStatusChangeBlock:(nullable void (^)(AFNetworkReachabilityStatus status))block;
  112. @end
  113. ///----------------
  114. /// @name Constants
  115. ///----------------
  116. /**
  117. ## Network Reachability
  118. The following constants are provided by `AFNetworkReachabilityManager` as possible network reachability statuses.
  119. enum {
  120. AFNetworkReachabilityStatusUnknown,
  121. AFNetworkReachabilityStatusNotReachable,
  122. AFNetworkReachabilityStatusReachableViaWWAN,
  123. AFNetworkReachabilityStatusReachableViaWiFi,
  124. }
  125. `AFNetworkReachabilityStatusUnknown`
  126. The `baseURL` host reachability is not known.
  127. `AFNetworkReachabilityStatusNotReachable`
  128. The `baseURL` host cannot be reached.
  129. `AFNetworkReachabilityStatusReachableViaWWAN`
  130. The `baseURL` host can be reached via a cellular connection, such as EDGE or GPRS.
  131. `AFNetworkReachabilityStatusReachableViaWiFi`
  132. The `baseURL` host can be reached via a Wi-Fi connection.
  133. ### Keys for Notification UserInfo Dictionary
  134. Strings that are used as keys in a `userInfo` dictionary in a network reachability status change notification.
  135. `AFNetworkingReachabilityNotificationStatusItem`
  136. A key in the userInfo dictionary in a `AFNetworkingReachabilityDidChangeNotification` notification.
  137. The corresponding value is an `NSNumber` object representing the `AFNetworkReachabilityStatus` value for the current reachability status.
  138. */
  139. ///--------------------
  140. /// @name Notifications
  141. ///--------------------
  142. /**
  143. Posted when network reachability changes.
  144. 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.
  145. @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`).
  146. */
  147. FOUNDATION_EXPORT NSString * const AFNetworkingReachabilityDidChangeNotification;
  148. FOUNDATION_EXPORT NSString * const AFNetworkingReachabilityNotificationStatusItem;
  149. ///--------------------
  150. /// @name Functions
  151. ///--------------------
  152. /**
  153. Returns a localized string representation of an `AFNetworkReachabilityStatus` value.
  154. */
  155. FOUNDATION_EXPORT NSString * AFStringFromNetworkReachabilityStatus(AFNetworkReachabilityStatus status);
  156. NS_ASSUME_NONNULL_END
  157. #endif