Refactored RKReachabilityObserver to work around issues with iOS 5 + host reachability and expand its capabilities. closes #408

* Added support for monitoring by IP address or hostname as well as local Wifi and Internet access generally
* Eliminated usage of synchronous calls to obtain reachability flags during status checks
* Reworked SystemConfiguration reachability callback to cache flags
* RKClient now monitors Internet access instead of hostname based reachability by default. baseURLReachabilityObserver eliminated in favor of reachabilityObserver. It is now a retain property that can be customized
* Queue suspension is now tied to the reachability observer rather than baseURL mutation
This commit is contained in:
Blake Watters
2011-10-18 00:43:25 -04:00
parent eb9dec5953
commit d75fd7d0b0
13 changed files with 951 additions and 492 deletions

View File

@@ -21,64 +21,197 @@
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
/**
* Posted when the network state has changed
*/
extern NSString* const RKReachabilityStateChangedNotification;
extern NSString* const RKReachabilityStateWasDeterminedNotification;
/// 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 {
RKReachabilityIndeterminate,
RKReachabilityNotReachable,
RKReachabilityReachableViaWiFi,
RKReachabilityReachableViaWWAN
RKReachabilityIndeterminate, // Network reachability not yet known
RKReachabilityNotReachable, // Network is not reachable
RKReachabilityReachableViaWiFi, // Network is reachable via a WiFi connection
RKReachabilityReachableViaWWAN // Network is reachable via a "wireless wide area network" (WWAN). i.e. GPRS, Edge, etc.
} RKReachabilityNetworkStatus;
/**
* Provides a notification based interface for monitoring changes
* to network status8
*
* 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
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 {
NSString* _hostName;
NSString* _host;
SCNetworkReachabilityRef _reachabilityRef;
BOOL _reachabilityEstablished;
BOOL _reachabilityDetermined;
BOOL _monitoringLocalWiFi;
SCNetworkReachabilityFlags _reachabilityFlags;
}
/**
The hostname we are observing reachability to
The remote hostname or IP address being observed for reachability.
*/
@property (nonatomic, readonly) NSString* hostName;
@property (nonatomic, readonly) NSString *host;
/**
Returns YES if reachability has been determined
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) BOOL reachabilityEstablished;
@property (nonatomic, readonly, getter=isReachabilityDetermined) BOOL reachabilityDetermined;
/**
* Create a new reachability observer against a given hostname. The observer
* will monitor the ability to reach the specified hostname and emit notifications
* when its reachability status changes.
*
* Note that the observer will be scheduled in the current run loop.
Returns YES if the reachability observer is monitoring the local WiFi interface
When the local WiFi interface is being monitored only three reachability states are possible:
RKReachabilityIndeterminate, RKReachabilityNotReachable, or RKReachabilityReachableViaWiFi.
If the device has connectivity through a WWAN connection only it will consider the network
not reachable.
@see reachabilityObserverForLocalWifi
*/
- (id)initWithHostname:(NSString*)hostName;
@property (nonatomic, readonly, getter=isMonitoringLocalWiFi) BOOL monitoringLocalWiFi;
/**
* Returns the current network status
Returns the current network status as determined by examining the state of the currently
cached reachabilityFlags
*/
- (RKReachabilityNetworkStatus)networkStatus;
@property (nonatomic, readonly) RKReachabilityNetworkStatus networkStatus;
/**
* Returns YES when the Internet is reachable (via WiFi or WWAN)
Returns 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;
////////////////////////////////////////////////////////////////////////////////////////////////
/**
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;
/**
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;
/**
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;
+ (RKReachabilityObserver *)reachabilityObserverForAddress:(const struct sockaddr *)address;
+ (RKReachabilityObserver *)reachabilityObserverForInternetAddress:(in_addr_t)internetAddress;
- (id)initWithHost:(NSString *)hostNameOrIPAddress;
- (id)initWithAddress:(const struct sockaddr *)address;
/**
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 WWAN may be available, but not active until a connection has been established.
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