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