2
0

WFAFNetworkReachabilityManager.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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, WFAFNetworkReachabilityStatus) {
  25. WFAFNetworkReachabilityStatusUnknown = -1,
  26. WFAFNetworkReachabilityStatusNotReachable = 0,
  27. WFAFNetworkReachabilityStatusReachableViaWWAN = 1,
  28. WFAFNetworkReachabilityStatusReachableViaWiFi = 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 WFAFNetworkReachabilityManager : NSObject
  38. /**
  39. The current network reachability status.
  40. */
  41. @property (readonly, nonatomic, assign) WFAFNetworkReachabilityStatus 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. * Initializes an instance of a network reachability manager
  86. *
  87. * @return nil as this method is unavailable
  88. */
  89. - (nullable instancetype)init NS_UNAVAILABLE;
  90. ///--------------------------------------------------
  91. /// @name Starting & Stopping Reachability Monitoring
  92. ///--------------------------------------------------
  93. /**
  94. Starts monitoring for changes in network reachability status.
  95. */
  96. - (void)startMonitoring;
  97. /**
  98. Stops monitoring for changes in network reachability status.
  99. */
  100. - (void)stopMonitoring;
  101. ///-------------------------------------------------
  102. /// @name Getting Localized Reachability Description
  103. ///-------------------------------------------------
  104. /**
  105. Returns a localized string representation of the current network reachability status.
  106. */
  107. - (NSString *)localizedNetworkReachabilityStatusString;
  108. ///---------------------------------------------------
  109. /// @name Setting Network Reachability Change Callback
  110. ///---------------------------------------------------
  111. /**
  112. Sets a callback to be executed when the network availability of the `baseURL` host changes.
  113. @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`.
  114. */
  115. - (void)setReachabilityStatusChangeBlock:(nullable void (^)(WFAFNetworkReachabilityStatus status))block;
  116. @end
  117. ///----------------
  118. /// @name Constants
  119. ///----------------
  120. /**
  121. ## Network Reachability
  122. The following constants are provided by `AFNetworkReachabilityManager` as possible network reachability statuses.
  123. enum {
  124. AFNetworkReachabilityStatusUnknown,
  125. AFNetworkReachabilityStatusNotReachable,
  126. AFNetworkReachabilityStatusReachableViaWWAN,
  127. AFNetworkReachabilityStatusReachableViaWiFi,
  128. }
  129. `AFNetworkReachabilityStatusUnknown`
  130. The `baseURL` host reachability is not known.
  131. `AFNetworkReachabilityStatusNotReachable`
  132. The `baseURL` host cannot be reached.
  133. `AFNetworkReachabilityStatusReachableViaWWAN`
  134. The `baseURL` host can be reached via a cellular connection, such as EDGE or GPRS.
  135. `AFNetworkReachabilityStatusReachableViaWiFi`
  136. The `baseURL` host can be reached via a Wi-Fi connection.
  137. ### Keys for Notification WFCCUserInfo Dictionary
  138. Strings that are used as keys in a `userInfo` dictionary in a network reachability status change notification.
  139. `AFNetworkingReachabilityNotificationStatusItem`
  140. A key in the userInfo dictionary in a `AFNetworkingReachabilityDidChangeNotification` notification.
  141. The corresponding value is an `NSNumber` object representing the `AFNetworkReachabilityStatus` value for the current reachability status.
  142. */
  143. ///--------------------
  144. /// @name Notifications
  145. ///--------------------
  146. /**
  147. Posted when network reachability changes.
  148. 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.
  149. @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`).
  150. */
  151. FOUNDATION_EXPORT NSString * const WFAFNetworkingReachabilityDidChangeNotification;
  152. FOUNDATION_EXPORT NSString * const WFAFNetworkingReachabilityNotificationStatusItem;
  153. ///--------------------
  154. /// @name Functions
  155. ///--------------------
  156. /**
  157. Returns a localized string representation of an `AFNetworkReachabilityStatus` value.
  158. */
  159. FOUNDATION_EXPORT NSString * WFAFStringFromNetworkReachabilityStatus(WFAFNetworkReachabilityStatus status);
  160. NS_ASSUME_NONNULL_END
  161. #endif