Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

executable file 314 lines (248 sloc) 11.03 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
//
// RKReachabilityObserver.h
// RestKit
//
// Created by Blake Watters on 9/14/10.
// Copyright (c) 2009-2012 RestKit. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>

///-----------------------------------------------------------------------------
/// @name Constants
///-----------------------------------------------------------------------------

/**
Posted when the network state has changed
*/
extern NSString * const RKReachabilityDidChangeNotification;

/**
User Info key for accessing the SCNetworkReachabilityFlags from a
RKReachabilityDidChangeNotification
*/
extern NSString * const RKReachabilityFlagsUserInfoKey;

/**
Posted when network state has been initially determined
*/
extern NSString * const RKReachabilityWasDeterminedNotification;

typedef enum {
    /**
Network reachability not yet known
*/
    RKReachabilityIndeterminate,
    /**
Network is not reachable
*/
    RKReachabilityNotReachable,
    /**
Network is reachable via a WiFi connection
*/
    RKReachabilityReachableViaWiFi,
    /**
Network is reachable via a "wireless wide area network" (WWAN). i.e. GPRS,
Edge, 3G, etc.
*/
    RKReachabilityReachableViaWWAN
} RKReachabilityNetworkStatus;

/**
Provides a notification based interface for monitoring changes
to network status.

When initialized, creates an SCReachabilityReg and schedules it for callback
notifications on the main dispatch queue. As notifications are intercepted from
SystemConfiguration, the observer will update its state and emit
`[RKReachabilityDidChangeNotifications](RKReachabilityDidChangeNotification)`
to inform listeners about state changes.

Portions of this software are derived from the Apple Reachability
code sample: http://developer.apple.com/library/ios/#samplecode/Reachability/Listings/Classes_Reachability_m.html
*/
@interface RKReachabilityObserver : NSObject {
    SCNetworkReachabilityRef _reachabilityRef;
}

///-----------------------------------------------------------------------------
/// @name Creating a Reachability Observer
///-----------------------------------------------------------------------------

/**
Creates and returns a RKReachabilityObserver instance observing reachability
changes to the hostname or IP address referenced in a given string. The
observer will monitor the ability to reach the specified remote host and emit
notifications when its reachability status changes.

The hostNameOrIPAddress will be introspected to determine if it contains an IP
address encoded into a string or a DNS name. The observer will be configured
appropriately based on the contents of the string.

@bug Note that iOS 5 has known issues with hostname based reachability
@param hostNameOrIPAddress An NSString containing a hostname or IP address to
be observed.
@return A reachability observer targeting the given hostname/IP address or nil
if it could not be observed.
*/
+ (RKReachabilityObserver *)reachabilityObserverForHost:(NSString *)hostNameOrIPAddress;

/**
Creates and returns a reachabilityObserverForInternet instance observing the
reachability to the Internet in general.

@return A reachability observer targeting INADDR_ANY or nil if it could not be
observed.
*/
+ (RKReachabilityObserver *)reachabilityObserverForInternet;

/**
Creates and returns a reachabilityObserverForInternet instance observing the
reachability to the Internet via the local WiFi interface. Internet access
available via the WWAN (3G, Edge, etc) will not be considered reachable.

@return A reachability observer targeting IN_LINKLOCALNETNUM or nil if it could
not be observed.
*/
+ (RKReachabilityObserver *)reachabilityObserverForLocalWifi;

/**
Creates and returns a RKReachabilityObserver instance observing reachability
changes to the sockaddr address provided.

@param address A socket address to determine reachability for.
@return A reachability observer targeting the given socket address or nil if it
could not be observed.
*/
+ (RKReachabilityObserver *)reachabilityObserverForAddress:(const struct sockaddr *)address;

/**
Creates and returns a RKReachabilityObserver instance observing reachability
changes to the IP address provided.

@param internetAddress A 32-bit integer representation of an IP address
@return A reachability observer targeting the given IP address or nil if it
could not be observed.
*/
+ (RKReachabilityObserver *)reachabilityObserverForInternetAddress:(in_addr_t)internetAddress;

/**
Returns a RKReachabilityObserver instance observing reachability changes to the
hostname or IP address referenced in a given string. The observer will monitor
the ability to reach the specified remote host and emit notifications when its
reachability status changes.

The hostNameOrIPAddress will be introspected to determine if it contains an IP
address encoded into a string or a DNS name. The observer will be configured
appropriately based on the contents of the string.

@bug Note that iOS 5 has known issues with hostname based reachability
@param hostNameOrIPAddress An NSString containing a hostname or IP address to
be observed.
@return A reachability observer targeting the given hostname/IP address or nil
if it could not be observed.
*/
- (id)initWithHost:(NSString *)hostNameOrIPAddress;

/**
Returns a RKReachabilityObserver instance observing reachability changes to the
sockaddr address provided.

@param address A socket address to determine reachability for.
@return A reachability observer targeting the given socket address or nil if it
could not be observed.
*/
- (id)initWithAddress:(const struct sockaddr *)address;


///-----------------------------------------------------------------------------
/// @name Determining the Host
///-----------------------------------------------------------------------------

/**
The remote hostname or IP address being observed for reachability.
*/
@property (nonatomic, readonly) NSString *host;


///-----------------------------------------------------------------------------
/// @name Managing Reachability States
///-----------------------------------------------------------------------------

/**
Current state of determining reachability

When initialized, RKReachabilityObserver instances are in an indeterminate
state to indicate that reachability status has not been yet established. After
the first callback is processed by the observer, the observer will answer YES
for reachabilityDetermined and networkStatus will return a determinate
response.

@return YES if reachability has been determined
*/
@property (nonatomic, readonly, getter=isReachabilityDetermined) BOOL reachabilityDetermined;

/**
Current network status as determined by examining the state of the currently
cached reachabilityFlags

@return Status of the network as RKReachabilityNetworkStatus
*/
@property (nonatomic, readonly) RKReachabilityNetworkStatus networkStatus;

/**
Current state of the local WiFi interface's reachability

When the local WiFi interface is being monitored, only three reachability
states are possible:

- RKReachabilityIndeterminate
- RKReachabilityNotReachable
- RKReachabilityReachableViaWiFi

If the device has connectivity through a WWAN connection only it will consider
the network not reachable.

@see reachabilityObserverForLocalWifi

@return YES if the reachability observer is monitoring the local WiFi interface
*/
@property (nonatomic, readonly, getter=isMonitoringLocalWiFi) BOOL monitoringLocalWiFi;


/**
The reachability flags as of the last invocation of the reachability callback

Each time the reachability callback is invoked with an asynchronous update of
reachability status the flags are cached and made accessible via the
reachabilityFlags method.

Flags can also be directly obtained via [RKReachabilityObserver getFlags]

@see getFlags
@return The most recently cached reachability flags reflecting current network
status.
*/
@property (nonatomic, readonly) SCNetworkReachabilityFlags reachabilityFlags;

/**
Acquires the current network reachability flags, answering YES if
successfully acquired; answering NO otherwise.

Beware! The System Configuration framework operates synchronously by
default. See Technical Q&A QA1693, Synchronous Networking On The Main
Thread. Asking for flags blocks the current thread and potentially kills your
iOS application if the reachability enquiry does not respond before the
watchdog times out.
*/
- (BOOL)getFlags;


///-----------------------------------------------------------------------------
/// @name Reachability Introspection
///-----------------------------------------------------------------------------

/**
Returns YES when the Internet is reachable (via WiFi or WWAN)

@exception NSInternalInconsistencyException Raises an
NSInternalInconsistencyException if called before reachability is determined
*/
- (BOOL)isNetworkReachable;

/**
Returns YES when we the network is reachable via WWAN

@exception NSInternalInconsistencyException Raises an
NSInternalInconsistencyException if called before reachability is determined
*/
- (BOOL)isReachableViaWWAN;

/**
Returns YES when we the network is reachable via WiFi

@exception NSInternalInconsistencyException Raises an
NSInternalInconsistencyException if called before reachability is determined
*/
- (BOOL)isReachableViaWiFi;

/**
Returns YES when WWAN may be available, but not active until a connection has been established.

@exception NSInternalInconsistencyException Raises an
NSInternalInconsistencyException if called before reachability is determined
*/
- (BOOL)isConnectionRequired;

/**
Returns YES if a dynamic, on-demand connection is available

@exception NSInternalInconsistencyException Raises an
NSInternalInconsistencyException if called before reachability is determined
*/
- (BOOL)isConnectionOnDemand;

/**
Returns YES if user intervention is required to initiate a connection

@exception NSInternalInconsistencyException Raises an
NSInternalInconsistencyException if called before reachability is determined
*/
- (BOOL)isInterventionRequired;

/**
Returns a string representation of the currently cached reachabilityFlags for inspection

@return A string containing single character representations of the bits in an
SCNetworkReachabilityFlags
*/
- (NSString *)reachabilityFlagsDescription;

@end
Something went wrong with that request. Please try again.