|
| 1 | +/* |
| 2 | + |
| 3 | + File: Reachability.h |
| 4 | + Abstract: Basic demonstration of how to use the SystemConfiguration Reachablity APIs. |
| 5 | + |
| 6 | + Version: 2.0.4ddg |
| 7 | + */ |
| 8 | + |
| 9 | +/* |
| 10 | + Significant additions made by Andrew W. Donoho, August 11, 2009. |
| 11 | + This is a derived work of Apple's Reachability v2.0 class. |
| 12 | + |
| 13 | + The below license is the new BSD license with the OSI recommended personalizations. |
| 14 | + <http://www.opensource.org/licenses/bsd-license.php> |
| 15 | + |
| 16 | + Extensions Copyright (C) 2009 Donoho Design Group, LLC. All Rights Reserved. |
| 17 | + |
| 18 | + Redistribution and use in source and binary forms, with or without |
| 19 | + modification, are permitted provided that the following conditions are |
| 20 | + met: |
| 21 | + |
| 22 | + * Redistributions of source code must retain the above copyright notice, |
| 23 | + this list of conditions and the following disclaimer. |
| 24 | + |
| 25 | + * Redistributions in binary form must reproduce the above copyright |
| 26 | + notice, this list of conditions and the following disclaimer in the |
| 27 | + documentation and/or other materials provided with the distribution. |
| 28 | + |
| 29 | + * Neither the name of Andrew W. Donoho nor Donoho Design Group, L.L.C. |
| 30 | + may be used to endorse or promote products derived from this software |
| 31 | + without specific prior written permission. |
| 32 | + |
| 33 | + THIS SOFTWARE IS PROVIDED BY DONOHO DESIGN GROUP, L.L.C. "AS IS" AND ANY |
| 34 | + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 35 | + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 36 | + PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 37 | + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 38 | + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 39 | + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 40 | + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 41 | + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 42 | + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 43 | + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 44 | + |
| 45 | + */ |
| 46 | + |
| 47 | + |
| 48 | +/* |
| 49 | + |
| 50 | + Apple's Original License on Reachability v2.0 |
| 51 | + |
| 52 | + Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc. |
| 53 | + ("Apple") in consideration of your agreement to the following terms, and your |
| 54 | + use, installation, modification or redistribution of this Apple software |
| 55 | + constitutes acceptance of these terms. If you do not agree with these terms, |
| 56 | + please do not use, install, modify or redistribute this Apple software. |
| 57 | + |
| 58 | + In consideration of your agreement to abide by the following terms, and subject |
| 59 | + to these terms, Apple grants you a personal, non-exclusive license, under |
| 60 | + Apple's copyrights in this original Apple software (the "Apple Software"), to |
| 61 | + use, reproduce, modify and redistribute the Apple Software, with or without |
| 62 | + modifications, in source and/or binary forms; provided that if you redistribute |
| 63 | + the Apple Software in its entirety and without modifications, you must retain |
| 64 | + this notice and the following text and disclaimers in all such redistributions |
| 65 | + of the Apple Software. |
| 66 | + |
| 67 | + Neither the name, trademarks, service marks or logos of Apple Inc. may be used |
| 68 | + to endorse or promote products derived from the Apple Software without specific |
| 69 | + prior written permission from Apple. Except as expressly stated in this notice, |
| 70 | + no other rights or licenses, express or implied, are granted by Apple herein, |
| 71 | + including but not limited to any patent rights that may be infringed by your |
| 72 | + derivative works or by other works in which the Apple Software may be |
| 73 | + incorporated. |
| 74 | + |
| 75 | + The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO |
| 76 | + WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED |
| 77 | + WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 78 | + PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN |
| 79 | + COMBINATION WITH YOUR PRODUCTS. |
| 80 | + |
| 81 | + IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR |
| 82 | + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 83 | + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 84 | + ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR |
| 85 | + DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF |
| 86 | + CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF |
| 87 | + APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 88 | + |
| 89 | + Copyright (C) 2009 Apple Inc. All Rights Reserved. |
| 90 | + |
| 91 | + */ |
| 92 | + |
| 93 | + |
| 94 | +/* |
| 95 | + DDG extensions include: |
| 96 | + Each reachability object now has a copy of the key used to store it in a |
| 97 | + dictionary. This allows each observer to quickly determine if the event is |
| 98 | + important to them. |
| 99 | + |
| 100 | + -currentReachabilityStatus also has a significantly different decision criteria than |
| 101 | + Apple's code. |
| 102 | + |
| 103 | + A multiple convenience test methods have been added. |
| 104 | + */ |
| 105 | + |
| 106 | +#import <Foundation/Foundation.h> |
| 107 | +#import <SystemConfiguration/SystemConfiguration.h> |
| 108 | + |
| 109 | +#define USE_DDG_EXTENSIONS 1 // Use DDG's Extensions to test network criteria. |
| 110 | +// Since NSAssert and NSCAssert are used in this code, |
| 111 | +// I recommend you set NS_BLOCK_ASSERTIONS=1 in the release versions of your projects. |
| 112 | + |
| 113 | +enum { |
| 114 | + |
| 115 | + // DDG NetworkStatus Constant Names. |
| 116 | + kNotReachable = 0, // Apple's code depends upon 'NotReachable' being the same value as 'NO'. |
| 117 | + kReachableViaWWAN, // Switched order from Apple's enum. WWAN is active before WiFi. |
| 118 | + kReachableViaWiFi |
| 119 | + |
| 120 | +}; |
| 121 | +typedef uint32_t NetworkStatus; |
| 122 | + |
| 123 | +enum { |
| 124 | + |
| 125 | + // Apple NetworkStatus Constant Names. |
| 126 | + NotReachable = kNotReachable, |
| 127 | + ReachableViaWiFi = kReachableViaWiFi, |
| 128 | + ReachableViaWWAN = kReachableViaWWAN |
| 129 | + |
| 130 | +}; |
| 131 | + |
| 132 | + |
| 133 | +extern NSString *const kInternetConnection; |
| 134 | +extern NSString *const kLocalWiFiConnection; |
| 135 | +extern NSString *const kReachabilityChangedNotification; |
| 136 | + |
| 137 | +@interface Reachability: NSObject { |
| 138 | + |
| 139 | +@private |
| 140 | + NSString *key_; |
| 141 | + SCNetworkReachabilityRef reachabilityRef; |
| 142 | + |
| 143 | +} |
| 144 | + |
| 145 | +@property (copy) NSString *key; // Atomic because network operations are asynchronous. |
| 146 | + |
| 147 | +// Designated Initializer. |
| 148 | +- (Reachability *) initWithReachabilityRef: (SCNetworkReachabilityRef) ref; |
| 149 | + |
| 150 | +// Use to check the reachability of a particular host name. |
| 151 | ++ (Reachability *) reachabilityWithHostName: (NSString*) hostName; |
| 152 | + |
| 153 | +// Use to check the reachability of a particular IP address. |
| 154 | ++ (Reachability *) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress; |
| 155 | + |
| 156 | +// Use to check whether the default route is available. |
| 157 | +// Should be used to, at minimum, establish network connectivity. |
| 158 | ++ (Reachability *) reachabilityForInternetConnection; |
| 159 | + |
| 160 | +// Use to check whether a local wifi connection is available. |
| 161 | ++ (Reachability *) reachabilityForLocalWiFi; |
| 162 | + |
| 163 | +//Start listening for reachability notifications on the current run loop. |
| 164 | +- (BOOL) startNotifier; |
| 165 | +- (void) stopNotifier; |
| 166 | + |
| 167 | +// Comparison routines to enable choosing actions in a notification. |
| 168 | +- (BOOL) isEqual: (Reachability *) r; |
| 169 | + |
| 170 | +// These are the status tests. |
| 171 | +- (NetworkStatus) currentReachabilityStatus; |
| 172 | + |
| 173 | +// The main direct test of reachability. |
| 174 | +- (BOOL) isReachable; |
| 175 | + |
| 176 | +// WWAN may be available, but not active until a connection has been established. |
| 177 | +// WiFi may require a connection for VPN on Demand. |
| 178 | +- (BOOL) isConnectionRequired; // Identical DDG variant. |
| 179 | +- (BOOL) connectionRequired; // Apple's routine. |
| 180 | + |
| 181 | +// Dynamic, on demand connection? |
| 182 | +- (BOOL) isConnectionOnDemand; |
| 183 | + |
| 184 | +// Is user intervention required? |
| 185 | +- (BOOL) isInterventionRequired; |
| 186 | + |
| 187 | +// Routines for specific connection testing by your app. |
| 188 | +- (BOOL) isReachableViaWWAN; |
| 189 | +- (BOOL) isReachableViaWiFi; |
| 190 | + |
| 191 | +- (SCNetworkReachabilityFlags) reachabilityFlags; |
| 192 | + |
| 193 | +@end |
0 commit comments