4 Abstract: Basic demonstration of how to use the SystemConfiguration Reachablity APIs.
10 Significant additions made by Andrew W. Donoho, August 11, 2009.
11 This is a derived work of Apple's Reachability v2.0 class.
13 The below license is the new BSD license with the OSI recommended personalizations.
14 <http://www.opensource.org/licenses/bsd-license.php>
16 Extensions Copyright (C) 2009 Donoho Design Group, LLC. All Rights Reserved.
18 Redistribution and use in source and binary forms, with or without
19 modification, are permitted provided that the following conditions are
22 * Redistributions of source code must retain the above copyright notice,
23 this list of conditions and the following disclaimer.
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.
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.
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.
50 Apple's Original License on Reachability v2.0
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.
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.
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
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.
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.
89 Copyright (C) 2009 Apple Inc. All Rights Reserved.
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
100 -currentReachabilityStatus also has a significantly different decision criteria than
103 A multiple convenience test methods have been added.
106 #import <Foundation/Foundation.h>
107 #import <SystemConfiguration/SystemConfiguration.h>
108 #import <netinet/in.h>
110 #define USE_DDG_EXTENSIONS 1 // Use DDG's Extensions to test network criteria.
111 // Since NSAssert and NSCAssert are used in this code,
112 // I recommend you set NS_BLOCK_ASSERTIONS=1 in the release versions of your projects.
116 // DDG NetworkStatus Constant Names.
117 kNotReachable = 0, // Apple's code depends upon 'NotReachable' being the same value as 'NO'.
118 kReachableViaWWAN, // Switched order from Apple's enum. WWAN is active before WiFi.
122 typedef uint32_t NetworkStatus;
126 // Apple NetworkStatus Constant Names.
127 NotReachable = kNotReachable,
128 ReachableViaWiFi = kReachableViaWiFi,
129 ReachableViaWWAN = kReachableViaWWAN
134 extern NSString *const kInternetConnection;
135 extern NSString *const kLocalWiFiConnection;
136 extern NSString *const kReachabilityChangedNotification;
138 @interface Reachability: NSObject {
142 SCNetworkReachabilityRef reachabilityRef;
146 @property (copy) NSString *key; // Atomic because network operations are asynchronous.
148 // Designated Initializer.
149 - (Reachability *) initWithReachabilityRef: (SCNetworkReachabilityRef) ref;
151 // Use to check the reachability of a particular host name.
152 + (Reachability *) reachabilityWithHostName: (NSString*) hostName;
154 // Use to check the reachability of a particular IP address.
155 + (Reachability *) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress;
157 // Use to check whether the default route is available.
158 // Should be used to, at minimum, establish network connectivity.
159 + (Reachability *) reachabilityForInternetConnection;
161 // Use to check whether a local wifi connection is available.
162 + (Reachability *) reachabilityForLocalWiFi;
164 //Start listening for reachability notifications on the current run loop.
165 - (BOOL) startNotifier;
166 - (void) stopNotifier;
168 // Comparison routines to enable choosing actions in a notification.
169 - (BOOL) isEqual: (Reachability *) r;
171 // These are the status tests.
172 - (NetworkStatus) currentReachabilityStatus;
174 // The main direct test of reachability.
175 - (BOOL) isReachable;
177 // WWAN may be available, but not active until a connection has been established.
178 // WiFi may require a connection for VPN on Demand.
179 - (BOOL) isConnectionRequired; // Identical DDG variant.
180 - (BOOL) connectionRequired; // Apple's routine.
182 // Dynamic, on demand connection?
183 - (BOOL) isConnectionOnDemand;
185 // Is user intervention required?
186 - (BOOL) isInterventionRequired;
188 // Routines for specific connection testing by your app.
189 - (BOOL) isReachableViaWWAN;
190 - (BOOL) isReachableViaWiFi;
192 - (SCNetworkReachabilityFlags) reachabilityFlags;