├── README.md └── Reachability_Demo ├── Reachability.h ├── Reachability.m ├── Reachability_Demo.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ ├── JanzTam.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── kuaikuaizuche.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ ├── JanzTam.xcuserdatad │ └── xcschemes │ │ ├── Reachability_Demo.xcscheme │ │ └── xcschememanagement.plist │ └── kuaikuaizuche.xcuserdatad │ └── xcschemes │ ├── Reachability_Demo.xcscheme │ └── xcschememanagement.plist └── Reachability_Demo ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist ├── ViewController.h ├── ViewController.m └── main.m /README.md: -------------------------------------------------------------------------------- 1 | # Reachability_Demo 2 | Reachability 使用 Demo,可检测2、3、4G 3 | -------------------------------------------------------------------------------- /Reachability_Demo/Reachability.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | Basic demonstration of how to use the SystemConfiguration Reachablity APIs. 7 | */ 8 | 9 | #import 10 | #import 11 | #import 12 | #import 13 | 14 | typedef enum : NSInteger { 15 | NotReachable = 0, 16 | ReachableViaWiFi, 17 | ReachableViaWWAN, 18 | kReachableVia2G, 19 | kReachableVia3G, 20 | kReachableVia4G 21 | } NetworkStatus; 22 | 23 | 24 | extern NSString *kReachabilityChangedNotification; 25 | 26 | 27 | @interface Reachability : NSObject 28 | 29 | /*! 30 | * Use to check the reachability of a given host name. 31 | */ 32 | + (instancetype)reachabilityWithHostName:(NSString *)hostName; 33 | 34 | /*! 35 | * Use to check the reachability of a given IP address. 36 | */ 37 | + (instancetype)reachabilityWithAddress:(const struct sockaddr_in *)hostAddress; 38 | 39 | /*! 40 | * Checks whether the default route is available. Should be used by applications that do not connect to a particular host. 41 | */ 42 | + (instancetype)reachabilityForInternetConnection; 43 | 44 | /*! 45 | * Checks whether a local WiFi connection is available. 46 | */ 47 | + (instancetype)reachabilityForLocalWiFi; 48 | 49 | /*! 50 | * Start listening for reachability notifications on the current run loop. 51 | */ 52 | - (BOOL)startNotifier; 53 | - (void)stopNotifier; 54 | 55 | - (NetworkStatus)currentReachabilityStatus; 56 | 57 | /*! 58 | * WWAN may be available, but not active until a connection has been established. WiFi may require a connection for VPN on Demand. 59 | */ 60 | - (BOOL)connectionRequired; 61 | 62 | @end 63 | 64 | 65 | -------------------------------------------------------------------------------- /Reachability_Demo/Reachability.m: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | Basic demonstration of how to use the SystemConfiguration Reachablity APIs. 7 | */ 8 | 9 | #import 10 | #import 11 | #import 12 | #import 13 | 14 | #import 15 | #import 16 | 17 | #import "Reachability.h" 18 | 19 | 20 | NSString *kReachabilityChangedNotification = @"kNetworkReachabilityChangedNotification"; 21 | 22 | 23 | #pragma mark - Supporting functions 24 | 25 | #define kShouldPrintReachabilityFlags 1 26 | 27 | static void PrintReachabilityFlags(SCNetworkReachabilityFlags flags, const char* comment) 28 | { 29 | #if kShouldPrintReachabilityFlags 30 | 31 | NSLog(@"Reachability Flag Status: %c%c %c%c%c%c%c%c%c %s\n", 32 | (flags & kSCNetworkReachabilityFlagsIsWWAN) ? 'W' : '-', 33 | (flags & kSCNetworkReachabilityFlagsReachable) ? 'R' : '-', 34 | 35 | (flags & kSCNetworkReachabilityFlagsTransientConnection) ? 't' : '-', 36 | (flags & kSCNetworkReachabilityFlagsConnectionRequired) ? 'c' : '-', 37 | (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) ? 'C' : '-', 38 | (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-', 39 | (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ? 'D' : '-', 40 | (flags & kSCNetworkReachabilityFlagsIsLocalAddress) ? 'l' : '-', 41 | (flags & kSCNetworkReachabilityFlagsIsDirect) ? 'd' : '-', 42 | comment 43 | ); 44 | #endif 45 | } 46 | 47 | 48 | static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info) 49 | { 50 | #pragma unused (target, flags) 51 | NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback"); 52 | NSCAssert([(__bridge NSObject*) info isKindOfClass: [Reachability class]], @"info was wrong class in ReachabilityCallback"); 53 | 54 | Reachability* noteObject = (__bridge Reachability *)info; 55 | // Post a notification to notify the client that the network reachability changed. 56 | [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification object: noteObject]; 57 | } 58 | 59 | 60 | #pragma mark - Reachability implementation 61 | 62 | @implementation Reachability 63 | { 64 | BOOL _alwaysReturnLocalWiFiStatus; //default is NO 65 | SCNetworkReachabilityRef _reachabilityRef; 66 | } 67 | 68 | + (instancetype)reachabilityWithHostName:(NSString *)hostName 69 | { 70 | Reachability* returnValue = NULL; 71 | SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]); 72 | if (reachability != NULL) 73 | { 74 | returnValue= [[self alloc] init]; 75 | if (returnValue != NULL) 76 | { 77 | returnValue->_reachabilityRef = reachability; 78 | returnValue->_alwaysReturnLocalWiFiStatus = NO; 79 | } 80 | else { 81 | CFRelease(reachability); 82 | } 83 | } 84 | return returnValue; 85 | } 86 | 87 | 88 | + (instancetype)reachabilityWithAddress:(const struct sockaddr_in *)hostAddress 89 | { 90 | SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)hostAddress); 91 | 92 | Reachability* returnValue = NULL; 93 | 94 | if (reachability != NULL) 95 | { 96 | returnValue = [[self alloc] init]; 97 | if (returnValue != NULL) 98 | { 99 | returnValue->_reachabilityRef = reachability; 100 | returnValue->_alwaysReturnLocalWiFiStatus = NO; 101 | } 102 | else { 103 | CFRelease(reachability); 104 | } 105 | } 106 | return returnValue; 107 | } 108 | 109 | 110 | 111 | + (instancetype)reachabilityForInternetConnection 112 | { 113 | struct sockaddr_in zeroAddress; 114 | bzero(&zeroAddress, sizeof(zeroAddress)); 115 | zeroAddress.sin_len = sizeof(zeroAddress); 116 | zeroAddress.sin_family = AF_INET; 117 | 118 | return [self reachabilityWithAddress:&zeroAddress]; 119 | } 120 | 121 | 122 | + (instancetype)reachabilityForLocalWiFi 123 | { 124 | struct sockaddr_in localWifiAddress; 125 | bzero(&localWifiAddress, sizeof(localWifiAddress)); 126 | localWifiAddress.sin_len = sizeof(localWifiAddress); 127 | localWifiAddress.sin_family = AF_INET; 128 | 129 | // IN_LINKLOCALNETNUM is defined in as 169.254.0.0. 130 | localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM); 131 | 132 | Reachability* returnValue = [self reachabilityWithAddress: &localWifiAddress]; 133 | if (returnValue != NULL) 134 | { 135 | returnValue->_alwaysReturnLocalWiFiStatus = YES; 136 | } 137 | 138 | return returnValue; 139 | } 140 | 141 | 142 | #pragma mark - Start and stop notifier 143 | 144 | - (BOOL)startNotifier 145 | { 146 | BOOL returnValue = NO; 147 | SCNetworkReachabilityContext context = {0, (__bridge void *)(self), NULL, NULL, NULL}; 148 | 149 | if (SCNetworkReachabilitySetCallback(_reachabilityRef, ReachabilityCallback, &context)) 150 | { 151 | if (SCNetworkReachabilityScheduleWithRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)) 152 | { 153 | returnValue = YES; 154 | } 155 | } 156 | 157 | return returnValue; 158 | } 159 | 160 | 161 | - (void)stopNotifier 162 | { 163 | if (_reachabilityRef != NULL) 164 | { 165 | SCNetworkReachabilityUnscheduleFromRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); 166 | } 167 | } 168 | 169 | 170 | - (void)dealloc 171 | { 172 | [self stopNotifier]; 173 | if (_reachabilityRef != NULL) 174 | { 175 | CFRelease(_reachabilityRef); 176 | } 177 | } 178 | 179 | 180 | #pragma mark - Network Flag Handling 181 | 182 | - (NetworkStatus)localWiFiStatusForFlags:(SCNetworkReachabilityFlags)flags 183 | { 184 | PrintReachabilityFlags(flags, "localWiFiStatusForFlags"); 185 | NetworkStatus returnValue = NotReachable; 186 | 187 | if ((flags & kSCNetworkReachabilityFlagsReachable) && (flags & kSCNetworkReachabilityFlagsIsDirect)) 188 | { 189 | returnValue = ReachableViaWiFi; 190 | } 191 | 192 | return returnValue; 193 | } 194 | 195 | 196 | - (NetworkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags 197 | { 198 | PrintReachabilityFlags(flags, "networkStatusForFlags"); 199 | if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) 200 | { 201 | // The target host is not reachable. 202 | return NotReachable; 203 | } 204 | 205 | NetworkStatus returnValue = NotReachable; 206 | 207 | if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0) 208 | { 209 | /* 210 | If the target host is reachable and no connection is required then we'll assume (for now) that you're on Wi-Fi... 211 | */ 212 | returnValue = ReachableViaWiFi; 213 | } 214 | 215 | if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) || 216 | (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0)) 217 | { 218 | /* 219 | ... and the connection is on-demand (or on-traffic) if the calling application is using the CFSocketStream or higher APIs... 220 | */ 221 | 222 | if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0) 223 | { 224 | /* 225 | ... and no [user] intervention is needed... 226 | */ 227 | returnValue = ReachableViaWiFi; 228 | } 229 | } 230 | 231 | if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN) 232 | { 233 | if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) 234 | { 235 | 236 | CTTelephonyNetworkInfo * info = [[CTTelephonyNetworkInfo alloc] init]; 237 | NSString *currentRadioAccessTechnology = info.currentRadioAccessTechnology; 238 | if (currentRadioAccessTechnology) 239 | { 240 | if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) 241 | { 242 | returnValue = kReachableVia4G; 243 | } 244 | else if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge] || [currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) 245 | { 246 | returnValue = kReachableVia2G; 247 | } 248 | else 249 | { 250 | returnValue = kReachableVia3G; 251 | } 252 | return returnValue; 253 | 254 | } 255 | } 256 | 257 | if ((flags & kSCNetworkReachabilityFlagsTransientConnection) == kSCNetworkReachabilityFlagsTransientConnection) 258 | { 259 | if((flags & kSCNetworkReachabilityFlagsConnectionRequired) == kSCNetworkReachabilityFlagsConnectionRequired) 260 | { 261 | returnValue = kReachableVia2G; 262 | return returnValue; 263 | } 264 | returnValue = kReachableVia3G; 265 | return returnValue; 266 | } 267 | 268 | returnValue = ReachableViaWWAN; 269 | } 270 | 271 | return returnValue; 272 | } 273 | 274 | 275 | - (BOOL)connectionRequired 276 | { 277 | NSAssert(_reachabilityRef != NULL, @"connectionRequired called with NULL reachabilityRef"); 278 | SCNetworkReachabilityFlags flags; 279 | 280 | if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags)) 281 | { 282 | return (flags & kSCNetworkReachabilityFlagsConnectionRequired); 283 | } 284 | 285 | return NO; 286 | } 287 | 288 | 289 | - (NetworkStatus)currentReachabilityStatus 290 | { 291 | NSAssert(_reachabilityRef != NULL, @"currentNetworkStatus called with NULL SCNetworkReachabilityRef"); 292 | NetworkStatus returnValue = NotReachable; 293 | SCNetworkReachabilityFlags flags; 294 | 295 | if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags)) 296 | { 297 | if (_alwaysReturnLocalWiFiStatus) 298 | { 299 | returnValue = [self localWiFiStatusForFlags:flags]; 300 | } 301 | else 302 | { 303 | returnValue = [self networkStatusForFlags:flags]; 304 | } 305 | } 306 | 307 | return returnValue; 308 | } 309 | 310 | 311 | @end 312 | -------------------------------------------------------------------------------- /Reachability_Demo/Reachability_Demo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 14CEB2931C10095D004EC196 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 14CEB2921C10095D004EC196 /* main.m */; }; 11 | 14CEB2961C10095D004EC196 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 14CEB2951C10095D004EC196 /* AppDelegate.m */; }; 12 | 14CEB2991C10095D004EC196 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 14CEB2981C10095D004EC196 /* ViewController.m */; }; 13 | 14CEB29C1C10095D004EC196 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 14CEB29A1C10095D004EC196 /* Main.storyboard */; }; 14 | 14CEB29E1C10095D004EC196 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 14CEB29D1C10095D004EC196 /* Assets.xcassets */; }; 15 | 14CEB2A11C10095D004EC196 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 14CEB29F1C10095D004EC196 /* LaunchScreen.storyboard */; }; 16 | 14CEB2AA1C100979004EC196 /* Reachability.m in Sources */ = {isa = PBXBuildFile; fileRef = 14CEB2A91C100979004EC196 /* Reachability.m */; }; 17 | /* End PBXBuildFile section */ 18 | 19 | /* Begin PBXFileReference section */ 20 | 14CEB28E1C10095D004EC196 /* Reachability_Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Reachability_Demo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 21 | 14CEB2921C10095D004EC196 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 22 | 14CEB2941C10095D004EC196 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 23 | 14CEB2951C10095D004EC196 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 24 | 14CEB2971C10095D004EC196 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 25 | 14CEB2981C10095D004EC196 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 26 | 14CEB29B1C10095D004EC196 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 27 | 14CEB29D1C10095D004EC196 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 28 | 14CEB2A01C10095D004EC196 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 29 | 14CEB2A21C10095D004EC196 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 30 | 14CEB2A81C100979004EC196 /* Reachability.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Reachability.h; sourceTree = ""; }; 31 | 14CEB2A91C100979004EC196 /* Reachability.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Reachability.m; sourceTree = ""; }; 32 | /* End PBXFileReference section */ 33 | 34 | /* Begin PBXFrameworksBuildPhase section */ 35 | 14CEB28B1C10095D004EC196 /* Frameworks */ = { 36 | isa = PBXFrameworksBuildPhase; 37 | buildActionMask = 2147483647; 38 | files = ( 39 | ); 40 | runOnlyForDeploymentPostprocessing = 0; 41 | }; 42 | /* End PBXFrameworksBuildPhase section */ 43 | 44 | /* Begin PBXGroup section */ 45 | 14CEB2851C10095D004EC196 = { 46 | isa = PBXGroup; 47 | children = ( 48 | 14CEB2A81C100979004EC196 /* Reachability.h */, 49 | 14CEB2A91C100979004EC196 /* Reachability.m */, 50 | 14CEB2901C10095D004EC196 /* Reachability_Demo */, 51 | 14CEB28F1C10095D004EC196 /* Products */, 52 | ); 53 | sourceTree = ""; 54 | }; 55 | 14CEB28F1C10095D004EC196 /* Products */ = { 56 | isa = PBXGroup; 57 | children = ( 58 | 14CEB28E1C10095D004EC196 /* Reachability_Demo.app */, 59 | ); 60 | name = Products; 61 | sourceTree = ""; 62 | }; 63 | 14CEB2901C10095D004EC196 /* Reachability_Demo */ = { 64 | isa = PBXGroup; 65 | children = ( 66 | 14CEB2941C10095D004EC196 /* AppDelegate.h */, 67 | 14CEB2951C10095D004EC196 /* AppDelegate.m */, 68 | 14CEB2971C10095D004EC196 /* ViewController.h */, 69 | 14CEB2981C10095D004EC196 /* ViewController.m */, 70 | 14CEB29A1C10095D004EC196 /* Main.storyboard */, 71 | 14CEB29D1C10095D004EC196 /* Assets.xcassets */, 72 | 14CEB29F1C10095D004EC196 /* LaunchScreen.storyboard */, 73 | 14CEB2A21C10095D004EC196 /* Info.plist */, 74 | 14CEB2911C10095D004EC196 /* Supporting Files */, 75 | ); 76 | path = Reachability_Demo; 77 | sourceTree = ""; 78 | }; 79 | 14CEB2911C10095D004EC196 /* Supporting Files */ = { 80 | isa = PBXGroup; 81 | children = ( 82 | 14CEB2921C10095D004EC196 /* main.m */, 83 | ); 84 | name = "Supporting Files"; 85 | sourceTree = ""; 86 | }; 87 | /* End PBXGroup section */ 88 | 89 | /* Begin PBXNativeTarget section */ 90 | 14CEB28D1C10095D004EC196 /* Reachability_Demo */ = { 91 | isa = PBXNativeTarget; 92 | buildConfigurationList = 14CEB2A51C10095D004EC196 /* Build configuration list for PBXNativeTarget "Reachability_Demo" */; 93 | buildPhases = ( 94 | 14CEB28A1C10095D004EC196 /* Sources */, 95 | 14CEB28B1C10095D004EC196 /* Frameworks */, 96 | 14CEB28C1C10095D004EC196 /* Resources */, 97 | ); 98 | buildRules = ( 99 | ); 100 | dependencies = ( 101 | ); 102 | name = Reachability_Demo; 103 | productName = Reachability_Demo; 104 | productReference = 14CEB28E1C10095D004EC196 /* Reachability_Demo.app */; 105 | productType = "com.apple.product-type.application"; 106 | }; 107 | /* End PBXNativeTarget section */ 108 | 109 | /* Begin PBXProject section */ 110 | 14CEB2861C10095D004EC196 /* Project object */ = { 111 | isa = PBXProject; 112 | attributes = { 113 | LastUpgradeCheck = 0710; 114 | ORGANIZATIONNAME = JanzTam; 115 | TargetAttributes = { 116 | 14CEB28D1C10095D004EC196 = { 117 | CreatedOnToolsVersion = 7.1.1; 118 | DevelopmentTeam = AGN67TQEF4; 119 | }; 120 | }; 121 | }; 122 | buildConfigurationList = 14CEB2891C10095D004EC196 /* Build configuration list for PBXProject "Reachability_Demo" */; 123 | compatibilityVersion = "Xcode 3.2"; 124 | developmentRegion = English; 125 | hasScannedForEncodings = 0; 126 | knownRegions = ( 127 | en, 128 | Base, 129 | ); 130 | mainGroup = 14CEB2851C10095D004EC196; 131 | productRefGroup = 14CEB28F1C10095D004EC196 /* Products */; 132 | projectDirPath = ""; 133 | projectRoot = ""; 134 | targets = ( 135 | 14CEB28D1C10095D004EC196 /* Reachability_Demo */, 136 | ); 137 | }; 138 | /* End PBXProject section */ 139 | 140 | /* Begin PBXResourcesBuildPhase section */ 141 | 14CEB28C1C10095D004EC196 /* Resources */ = { 142 | isa = PBXResourcesBuildPhase; 143 | buildActionMask = 2147483647; 144 | files = ( 145 | 14CEB2A11C10095D004EC196 /* LaunchScreen.storyboard in Resources */, 146 | 14CEB29E1C10095D004EC196 /* Assets.xcassets in Resources */, 147 | 14CEB29C1C10095D004EC196 /* Main.storyboard in Resources */, 148 | ); 149 | runOnlyForDeploymentPostprocessing = 0; 150 | }; 151 | /* End PBXResourcesBuildPhase section */ 152 | 153 | /* Begin PBXSourcesBuildPhase section */ 154 | 14CEB28A1C10095D004EC196 /* Sources */ = { 155 | isa = PBXSourcesBuildPhase; 156 | buildActionMask = 2147483647; 157 | files = ( 158 | 14CEB2991C10095D004EC196 /* ViewController.m in Sources */, 159 | 14CEB2961C10095D004EC196 /* AppDelegate.m in Sources */, 160 | 14CEB2AA1C100979004EC196 /* Reachability.m in Sources */, 161 | 14CEB2931C10095D004EC196 /* main.m in Sources */, 162 | ); 163 | runOnlyForDeploymentPostprocessing = 0; 164 | }; 165 | /* End PBXSourcesBuildPhase section */ 166 | 167 | /* Begin PBXVariantGroup section */ 168 | 14CEB29A1C10095D004EC196 /* Main.storyboard */ = { 169 | isa = PBXVariantGroup; 170 | children = ( 171 | 14CEB29B1C10095D004EC196 /* Base */, 172 | ); 173 | name = Main.storyboard; 174 | sourceTree = ""; 175 | }; 176 | 14CEB29F1C10095D004EC196 /* LaunchScreen.storyboard */ = { 177 | isa = PBXVariantGroup; 178 | children = ( 179 | 14CEB2A01C10095D004EC196 /* Base */, 180 | ); 181 | name = LaunchScreen.storyboard; 182 | sourceTree = ""; 183 | }; 184 | /* End PBXVariantGroup section */ 185 | 186 | /* Begin XCBuildConfiguration section */ 187 | 14CEB2A31C10095D004EC196 /* Debug */ = { 188 | isa = XCBuildConfiguration; 189 | buildSettings = { 190 | ALWAYS_SEARCH_USER_PATHS = NO; 191 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 192 | CLANG_CXX_LIBRARY = "libc++"; 193 | CLANG_ENABLE_MODULES = YES; 194 | CLANG_ENABLE_OBJC_ARC = YES; 195 | CLANG_WARN_BOOL_CONVERSION = YES; 196 | CLANG_WARN_CONSTANT_CONVERSION = YES; 197 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 198 | CLANG_WARN_EMPTY_BODY = YES; 199 | CLANG_WARN_ENUM_CONVERSION = YES; 200 | CLANG_WARN_INT_CONVERSION = YES; 201 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 202 | CLANG_WARN_UNREACHABLE_CODE = YES; 203 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 204 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 205 | COPY_PHASE_STRIP = NO; 206 | DEBUG_INFORMATION_FORMAT = dwarf; 207 | ENABLE_STRICT_OBJC_MSGSEND = YES; 208 | ENABLE_TESTABILITY = YES; 209 | GCC_C_LANGUAGE_STANDARD = gnu99; 210 | GCC_DYNAMIC_NO_PIC = NO; 211 | GCC_NO_COMMON_BLOCKS = YES; 212 | GCC_OPTIMIZATION_LEVEL = 0; 213 | GCC_PREPROCESSOR_DEFINITIONS = ( 214 | "DEBUG=1", 215 | "$(inherited)", 216 | ); 217 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 218 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 219 | GCC_WARN_UNDECLARED_SELECTOR = YES; 220 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 221 | GCC_WARN_UNUSED_FUNCTION = YES; 222 | GCC_WARN_UNUSED_VARIABLE = YES; 223 | IPHONEOS_DEPLOYMENT_TARGET = 9.1; 224 | MTL_ENABLE_DEBUG_INFO = YES; 225 | ONLY_ACTIVE_ARCH = YES; 226 | SDKROOT = iphoneos; 227 | }; 228 | name = Debug; 229 | }; 230 | 14CEB2A41C10095D004EC196 /* Release */ = { 231 | isa = XCBuildConfiguration; 232 | buildSettings = { 233 | ALWAYS_SEARCH_USER_PATHS = NO; 234 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 235 | CLANG_CXX_LIBRARY = "libc++"; 236 | CLANG_ENABLE_MODULES = YES; 237 | CLANG_ENABLE_OBJC_ARC = YES; 238 | CLANG_WARN_BOOL_CONVERSION = YES; 239 | CLANG_WARN_CONSTANT_CONVERSION = YES; 240 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 241 | CLANG_WARN_EMPTY_BODY = YES; 242 | CLANG_WARN_ENUM_CONVERSION = YES; 243 | CLANG_WARN_INT_CONVERSION = YES; 244 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 245 | CLANG_WARN_UNREACHABLE_CODE = YES; 246 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 247 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 248 | COPY_PHASE_STRIP = NO; 249 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 250 | ENABLE_NS_ASSERTIONS = NO; 251 | ENABLE_STRICT_OBJC_MSGSEND = YES; 252 | GCC_C_LANGUAGE_STANDARD = gnu99; 253 | GCC_NO_COMMON_BLOCKS = YES; 254 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 255 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 256 | GCC_WARN_UNDECLARED_SELECTOR = YES; 257 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 258 | GCC_WARN_UNUSED_FUNCTION = YES; 259 | GCC_WARN_UNUSED_VARIABLE = YES; 260 | IPHONEOS_DEPLOYMENT_TARGET = 9.1; 261 | MTL_ENABLE_DEBUG_INFO = NO; 262 | SDKROOT = iphoneos; 263 | VALIDATE_PRODUCT = YES; 264 | }; 265 | name = Release; 266 | }; 267 | 14CEB2A61C10095D004EC196 /* Debug */ = { 268 | isa = XCBuildConfiguration; 269 | buildSettings = { 270 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 271 | CODE_SIGN_IDENTITY = "iPhone Developer"; 272 | INFOPLIST_FILE = Reachability_Demo/Info.plist; 273 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 274 | PRODUCT_BUNDLE_IDENTIFIER = "com.JanzTam.Reachability-Demo"; 275 | PRODUCT_NAME = "$(TARGET_NAME)"; 276 | }; 277 | name = Debug; 278 | }; 279 | 14CEB2A71C10095D004EC196 /* Release */ = { 280 | isa = XCBuildConfiguration; 281 | buildSettings = { 282 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 283 | CODE_SIGN_IDENTITY = "iPhone Developer"; 284 | INFOPLIST_FILE = Reachability_Demo/Info.plist; 285 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 286 | PRODUCT_BUNDLE_IDENTIFIER = "com.JanzTam.Reachability-Demo"; 287 | PRODUCT_NAME = "$(TARGET_NAME)"; 288 | }; 289 | name = Release; 290 | }; 291 | /* End XCBuildConfiguration section */ 292 | 293 | /* Begin XCConfigurationList section */ 294 | 14CEB2891C10095D004EC196 /* Build configuration list for PBXProject "Reachability_Demo" */ = { 295 | isa = XCConfigurationList; 296 | buildConfigurations = ( 297 | 14CEB2A31C10095D004EC196 /* Debug */, 298 | 14CEB2A41C10095D004EC196 /* Release */, 299 | ); 300 | defaultConfigurationIsVisible = 0; 301 | defaultConfigurationName = Release; 302 | }; 303 | 14CEB2A51C10095D004EC196 /* Build configuration list for PBXNativeTarget "Reachability_Demo" */ = { 304 | isa = XCConfigurationList; 305 | buildConfigurations = ( 306 | 14CEB2A61C10095D004EC196 /* Debug */, 307 | 14CEB2A71C10095D004EC196 /* Release */, 308 | ); 309 | defaultConfigurationIsVisible = 0; 310 | defaultConfigurationName = Release; 311 | }; 312 | /* End XCConfigurationList section */ 313 | }; 314 | rootObject = 14CEB2861C10095D004EC196 /* Project object */; 315 | } 316 | -------------------------------------------------------------------------------- /Reachability_Demo/Reachability_Demo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Reachability_Demo/Reachability_Demo.xcodeproj/project.xcworkspace/xcuserdata/JanzTam.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanzTam/Reachability_Demo/fe2b2b17f83edcd21e3e53305af4b965c5d6a367/Reachability_Demo/Reachability_Demo.xcodeproj/project.xcworkspace/xcuserdata/JanzTam.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Reachability_Demo/Reachability_Demo.xcodeproj/project.xcworkspace/xcuserdata/kuaikuaizuche.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JanzTam/Reachability_Demo/fe2b2b17f83edcd21e3e53305af4b965c5d6a367/Reachability_Demo/Reachability_Demo.xcodeproj/project.xcworkspace/xcuserdata/kuaikuaizuche.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Reachability_Demo/Reachability_Demo.xcodeproj/xcuserdata/JanzTam.xcuserdatad/xcschemes/Reachability_Demo.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /Reachability_Demo/Reachability_Demo.xcodeproj/xcuserdata/JanzTam.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | Reachability_Demo.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 14CEB28D1C10095D004EC196 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /Reachability_Demo/Reachability_Demo.xcodeproj/xcuserdata/kuaikuaizuche.xcuserdatad/xcschemes/Reachability_Demo.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /Reachability_Demo/Reachability_Demo.xcodeproj/xcuserdata/kuaikuaizuche.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | Reachability_Demo.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 14CEB28D1C10095D004EC196 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /Reachability_Demo/Reachability_Demo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // Reachability_Demo 4 | // 5 | // Created by kuaikuaizuche on 15/12/3. 6 | // Copyright © 2015年 JanzTam. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "Reachability.h" 11 | 12 | @interface AppDelegate : UIResponder 13 | 14 | @property (strong, nonatomic) UIWindow *window; 15 | 16 | @property (strong,nonatomic) Reachability * hostReach; 17 | 18 | @end 19 | 20 | -------------------------------------------------------------------------------- /Reachability_Demo/Reachability_Demo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // Reachability_Demo 4 | // 5 | // Created by kuaikuaizuche on 15/12/3. 6 | // Copyright © 2015年 JanzTam. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | [self addReachability]; 21 | return YES; 22 | } 23 | 24 | - (void)addReachability { 25 | // 监测网络情况 26 | [[NSNotificationCenter defaultCenter] addObserver:self 27 | selector:@selector(reachabilityChanged:) 28 | name: kReachabilityChangedNotification 29 | object: nil]; 30 | _hostReach = [Reachability reachabilityWithHostName:@"https://www.baidu.com/"]; 31 | [_hostReach startNotifier]; 32 | } 33 | 34 | - (void)reachabilityChanged:(NSNotification *)note 35 | { 36 | Reachability* curReach = [note object]; 37 | NSParameterAssert([curReach isKindOfClass: [Reachability class]]); 38 | NetworkStatus status = [curReach currentReachabilityStatus]; 39 | 40 | switch (status) 41 | { 42 | case NotReachable: 43 | break; 44 | 45 | case ReachableViaWiFi: 46 | break; 47 | case ReachableViaWWAN: 48 | case kReachableVia2G: 49 | break; 50 | 51 | case kReachableVia3G: 52 | break; 53 | 54 | case kReachableVia4G: 55 | break; 56 | } 57 | } 58 | 59 | - (void)applicationWillResignActive:(UIApplication *)application { 60 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 61 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 62 | } 63 | 64 | - (void)applicationDidEnterBackground:(UIApplication *)application { 65 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 66 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 67 | } 68 | 69 | - (void)applicationWillEnterForeground:(UIApplication *)application { 70 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 71 | } 72 | 73 | - (void)applicationDidBecomeActive:(UIApplication *)application { 74 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 75 | } 76 | 77 | - (void)applicationWillTerminate:(UIApplication *)application { 78 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 79 | } 80 | 81 | @end 82 | -------------------------------------------------------------------------------- /Reachability_Demo/Reachability_Demo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /Reachability_Demo/Reachability_Demo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /Reachability_Demo/Reachability_Demo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Reachability_Demo/Reachability_Demo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Reachability_Demo/Reachability_Demo/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // Reachability_Demo 4 | // 5 | // Created by kuaikuaizuche on 15/12/3. 6 | // Copyright © 2015年 JanzTam. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | - (IBAction)getCurrentReachability:(UIButton *)sender; 14 | 15 | @end 16 | 17 | -------------------------------------------------------------------------------- /Reachability_Demo/Reachability_Demo/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // Reachability_Demo 4 | // 5 | // Created by kuaikuaizuche on 15/12/3. 6 | // Copyright © 2015年 JanzTam. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "Reachability.h" 11 | 12 | @interface ViewController () 13 | { 14 | UIAlertController * ac; 15 | } 16 | @end 17 | 18 | @implementation ViewController 19 | 20 | - (void)viewDidLoad { 21 | [super viewDidLoad]; 22 | 23 | } 24 | 25 | - (void)didReceiveMemoryWarning { 26 | [super didReceiveMemoryWarning]; 27 | // Dispose of any resources that can be recreated. 28 | } 29 | 30 | - (IBAction)getCurrentReachability:(UIButton *)sender { 31 | Reachability * reach = [Reachability reachabilityForInternetConnection]; 32 | NSString * tips = @""; 33 | switch (reach.currentReachabilityStatus) 34 | { 35 | case NotReachable: 36 | tips = @"无网络连接"; 37 | break; 38 | 39 | case ReachableViaWiFi: 40 | tips = @"Wifi"; 41 | break; 42 | 43 | case ReachableViaWWAN: 44 | NSLog(@"移动流量"); 45 | case kReachableVia2G: 46 | tips = @"2G"; 47 | break; 48 | 49 | case kReachableVia3G: 50 | tips = @"3G"; 51 | break; 52 | 53 | case kReachableVia4G: 54 | tips = @"4G"; 55 | break; 56 | } 57 | NSLog(@"tips = %@",tips); 58 | ac = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"当前网络状态为:%@",tips] preferredStyle:UIAlertControllerStyleAlert]; 59 | UIAlertAction * aa = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 60 | [ac dismissViewControllerAnimated:YES completion:nil]; 61 | ac = nil; 62 | }]; 63 | [ac addAction:aa]; 64 | [self presentViewController:ac animated:YES completion:nil]; 65 | } 66 | @end 67 | -------------------------------------------------------------------------------- /Reachability_Demo/Reachability_Demo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // Reachability_Demo 4 | // 5 | // Created by kuaikuaizuche on 15/12/3. 6 | // Copyright © 2015年 JanzTam. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | --------------------------------------------------------------------------------