AFNetworkReachabilityManager.h 7.8 KB

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