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